Reputation: 747
I am working on google maps. I want to get the user location and show him on the map. I wrote a Locationlistener to get the user location.
public Location getUserLocation(){
try {
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
return null;
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,DISTANCE_CALC,TIME_UPDATE, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,DISTANCE_CALC,TIME_UPDATE, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
The getUserLocation()
is one of the functions in the LocationListener
that will return the user location with either the GPS or the network. I am calling this function from a fragment like this.
if (Build.VERSION.SDK_INT >= 23){
if (checkPermission()) {
location = locHandler.getUserLocation();
if(location!=null)
showTheMaps(location);
else
AlertBuilder(title,body);
}else {
requestPermission();
}
}else {
//The build is less than marshmellow so no need for permission
location = locHandler.getUserLocation();
try {
// double d = location.getLatitude();
showTheMaps(location);
}catch (NullPointerException e){
AlertBuilder(title, body);
}
}
public void showTheMaps(Location location){
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()), DataModel.zoomlevel);
googleMap.animateCamera(cameraUpdate);
}
The AlertBuilder
function will show an alert box with a button which upon click closes the app.
As you can see from the screenshot the GPS is enabled yet the location returned to me is null and the app closes after showing the alert dialog.
What could be the problem? why is the location returning null even after turning on the GPS?
Upvotes: 1
Views: 1704
Reputation: 1783
Use
Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
Source: https://developer.android.com/training/location/retrieve-current.html
Another (deprecated) way is to set a OnMyLocationChangeListener
to your map to get the location
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
// do something with location
}
});
More info: how to get current location in google map android
Upvotes: 1