I read posts like this, android getlastknownlocation returns null, but I have a problem apart from the known location.
i know that if i don't have any provider, i can't have any known location, getLastKnowedLocation(). but, How i can get the location when the provider is detected?
i use this code:
public void myPosition() {
if (mapa.getMyLocation() != null)
mapa.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng( mapa.getMyLocation().getLatitude(), mapa.getMyLocation().getLongitude()), 13));
else{
loca = locaMan.getLastKnownLocation(bestProvi);
if (loca != null)
mapa.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng( loca.getLatitude(), loca.getLongitude()), 15));
}
}
public void onLocationChanged(Location location) {
myPosition();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
myPosition();
}
@Override
public void onProviderEnabled(String provider) {
myPosition();
Toast toast1 =Toast.makeText(getApplicationContext(),"Provider ON", Toast.LENGTH_SHORT);
toast1.show();
}
@Override
public void onProviderDisabled(String provider) {
Toast toast1 =Toast.makeText(getApplicationContext(),"Provider OFF", Toast.LENGTH_SHORT);
toast1.show();
}
But when the provider is activated, it isn't execute the code of method onProviderEnabled(), only when i turn off and turn on GPS on my smartphone.
When i start the aplication, i see a map, and 5 seconds after i see a blue point over my position, but myPosition() method isn't executed.
How i can do this?
Thank you for all.