0


I've simply made an app that shows my current location co-ordinates. But if I change my location then it is shows my old location co-ordinates. Again if I reboot my device it shows the right co-ordinates.
Here is a part of my code where I got my co-ordinates:

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BWN_UPDATE,MIN_DIST_CHANGE_FOR_UPDATES,this);

                if (locationManager != null){
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                }
                if (location != null){
                    longitude = location.getLongitude();
                    latitude = location.getLatitude();
                }
PPB
  • 151
  • 1
  • 14

3 Answers3

0

getLastKnownLocation() isn't good for getting accurate results instantly since it works without waiting for GPS fix - it just gives you location, which is cached by system (you could try running other location using app e.g. Google Maps, then running your and location will be newer).

To make it work as expected you could use onLocationChanged method of LocationListener interface - try one explained here How to get current location in Android

Community
  • 1
  • 1
0

The GPS function takes its time to identify your location on request.

While you are changing you location, you need to wait for time that GPS set new location. Till that time, LocationManager will return your old location in :

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

In Samsung Galaxy devices, when you start GPS (Location) button, the icon start blinking in statusbar area. This is to tell user that GPS is searching new location now. When new location is found then the icon will stop blinking and notification will appear "Location set by GPS" Technically, you will get callback onLocationChanged() at this time. For this callback, you need to implement LocationListener

When you are rebooting device, GPS has got sufficient time to set new location so it is giving you correct location

Kushal
  • 8,100
  • 9
  • 63
  • 82
0

Use onLocationChanged() callback instead of getLastKnownLocation(). Here is an example code :

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    // Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            if (location != null){
                longitude = location.getLongitude();
                latitude = location.getLatitude();
            }


        }
    };

How and when to register Location Listener?

If you want location listener for display current location on map then you should register Location Listener in onCreate() or onResume() method of the activity and unregister receiver in onPause() or onStop() method. To unregister location updates you can use the below method.

locationManager.removeUpdates(locationListener) 

Also location lock by GPS is always not possible so I will suggest you to use Network provider too. Just add this statement.

  locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BWN_UPDATE,MIN_DIST_CHANGE_FOR_UPDATES,locationListener);

and do this change to your GPS registration statement by putting locationListener instead of this as your third argument of locationManager.requestLocationUpdates();

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BWN_UPDATE,MIN_DIST_CHANGE_FOR_UPDATES,locationListener);
Exception
  • 2,273
  • 1
  • 24
  • 42
  • I'm still not getting how to implement LocationListener. Can you provide more details. @Best_of_his_name – PPB Mar 30 '15 at 11:53
  • Go through [this](https://sites.google.com/site/androidhowto/how-to-1/get-notified-when-location-changes) example. – Exception Mar 30 '15 at 11:59
  • [this](http://examples.javacodegeeks.com/android/core/location/android-location-based-services-example/) is a little better than the above one. It almost has the complete code with the proper explanation of which part to put where – Exception Mar 30 '15 at 12:04
  • don't know why the 2nd link doesn't responded. but i found the solution by using [Fused Location Provider.](http://javapapers.com/android/android-location-fused-provider/).Thank you@Best_of_his_name – PPB Apr 02 '15 at 11:40
  • Great!!! And thanks for sharing your link. I hope it helps anyone else who face such issue @ Partha – Exception Apr 02 '15 at 12:04