Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getLastKnownLocation() always return the same location

I'm tring to get the location from the gps/netwrok by best provider but always it's return the same location, also i can see that on google map there is a sign to my right location. please anyone can tell me what i'm doing wrong?

My activity is:

 public class MainMenu2 extends Activity implements LocationListener, OnClickListener

on my onCreate i have:

 if(isGooglePlay()){
        setContentView(R.layout.menu_2);
        setUpMapIfNeeded();
    }

isGooglePlay method:

 private boolean isGooglePlay() { // looks if googlemaps is available

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if(status == ConnectionResult.SUCCESS){
        return true;
    }
    else{
        ((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 10)).show();
        //Toast.makeText(this, "Google Play is not available", Toast.LENGTH_SHORT).show();
        return(false);
    }

}//isGooglePlay

setUpMapIfNeeded method:

   private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.menu_map))
                        .getMap();
    // Check if we were successful in obtaining the map.
    if (mMap != null) {
        // The Map is verified. It is now safe to manipulate the map.
        mMap.setMyLocationEnabled(true);

        locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria(); 
        criteria.setAccuracy(ACCURACY_FINE); 
        provider = locationManager.getBestProvider(criteria, true);

        if (provider == null){
            onProviderDisabled(provider);
        }
        locationManager.requestLocationUpdates(provider, 100, 1, this);
        loc = locationManager.getLastKnownLocation(provider);
            //---here the privider is "gps" but always the same location---//
        if (loc != null){
            onLocationChanged(loc);
        }
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); // set Map Type
    }
}
 }//setUpMap

and override methods:

 @Override
 public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
LatLng latlng = new LatLng(location.getLatitude(),location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));

double pLong = location.getLongitude();
double pLat = location.getLatitude();
Latstr = String.valueOf(pLong);
Lngstr = String.valueOf(pLat);
latitude = pLat;
longitude = pLong;
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Please turn ON the GPS", Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
protected void onResume() {
    super.onResume();
    myLatLng = new LatLng(latitude,longitude);
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(myLatLng, 15);
    mMap.animateCamera(update);
    mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude )).title("Find me here!"));


}

i also tried to search here for similer problem but i didn't got nothing..

like image 785
elik_sr Avatar asked Jun 22 '13 12:06

elik_sr


1 Answers

There is a bug with LocationProvider on some platforms like Samsung, custom Android build. You need to call this code (which actually does nothing), which triggers the phone's background cache to update its current location.

If you dont believe me, change your Gps position to a different location about 200m away, load your app, you dont notice the location gps change. Now, just load Maps app on your phone and suddenly your app will show current location.

To do this trigger programmatically put these lines just before your getLastKnownLocation call.

HomeScreen.getLocationManager().requestLocationUpdates(
    LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
        @Override
        public void onProviderEnabled(String provider) {
        }
        @Override
        public void onProviderDisabled(String provider) {
        }
        @Override
        public void onLocationChanged(final Location location) {
        }
    });

Edit

OR

Use the new api's LocationClient instead of LocationProvider.

like image 109
Siddharth Avatar answered Sep 18 '22 17:09

Siddharth