Getlastlocation() Always Null In Googleapiclient
Solution 1:
You need to call requestLocationUpdates()
in order to register the listener and have onLocationChanged()
invoked.
Be sure to un-register the listener as soon as possible to avoid excessive battery drain.
Also note that the getLastLocation() method can and will return null. The main problem is that it doesn't prompt a request to the OS for a new location lock, instead it just checks if there was a last known location from some other app's location request. If no other app had recently made a location request, then you get a null location returned to you.
The only way to guarantee that you actually get a location is to request one, and this is done with a call to requestLocationUpdates().
Here is a working example for reference:
publicclassMainActivityextendsFragmentActivityimplementsOnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
LocationListener {
privateGoogleMap map;
privateLocationRequest mLocationRequest;
privateGoogleApiClient mGoogleApiClient;
privateLocation mLastLocation;
privateMarker marker;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@OverrideprotectedvoidonResume() {
super.onResume();
buildGoogleApiClient();
mGoogleApiClient.connect();
if (map == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
}
@OverridepublicvoidonMapReady(GoogleMap retMap) {
map = retMap;
setUpMap();
}
publicvoidsetUpMap(){
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
}
@OverrideprotectedvoidonPause(){
super.onPause();
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
protected synchronized voidbuildGoogleApiClient() {
Toast.makeText(this, "buildGoogleApiClient", Toast.LENGTH_SHORT).show();
mGoogleApiClient = newGoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@OverridepublicvoidonConnected(Bundle bundle) {
Toast.makeText(this,"onConnected",Toast.LENGTH_SHORT).show();
mLocationRequest = newLocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
//mLocationRequest.setSmallestDisplacement(0.1F);LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@OverridepublicvoidonConnectionSuspended(int i) {
Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonLocationChanged(Location location) {
mLastLocation = location;
//remove previous current location Markerif (marker != null){
marker.remove();
}
double dLatitude = mLastLocation.getLatitude();
double dLongitude = mLastLocation.getLongitude();
marker = map.addMarker(newMarkerOptions().position(newLatLng(dLatitude, dLongitude))
.title("My Location").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(newLatLng(dLatitude, dLongitude), 8));
}
}
One more thing, if your map is in a Fragment, there is no need to have a nested SupportMapFragment. You can just have your Fragment extend SupportMapFragment. This removes the need of having a nested Fragment, and you don't even need to inflate any layout xml, here is a simple example:
publicclassMapTabFragmentextendsSupportMapFragmentimplementsOnMapReadyCallback {
privateGoogleMap mMap;
privateMarker marker;
publicMapTabFragment() {
}
@OverridepublicvoidonResume() {
super.onResume();
setUpMapIfNeeded();
}
privatevoidsetUpMapIfNeeded() {
if (mMap == null) {
getMapAsync(this);
}
}
@OverridepublicvoidonMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();
}
privatevoidsetUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.getUiSettings().setMapToolbarEnabled(false);
mMap.setOnMapClickListener(newGoogleMap.OnMapClickListener() {
@OverridepublicvoidonMapClick(LatLng point) {
//remove previously placed Markerif (marker != null) {
marker.remove();
}
//place marker where user just clicked
marker = mMap.addMarker(newMarkerOptions().position(point).title("Marker")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
});
}
}
Post a Comment for "Getlastlocation() Always Null In Googleapiclient"