2

I want the exact location of user. So I created two listeners as:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGPSListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mNetworkListener);

I am listening for location updates from both the network and gps providers and wanted to check the accurary of locations obtained by both so that I can pick the accurate location. I just want to ask am I using the right way of acheiving that...??? if not, please provide some guideline how to do this...???

My example code is:

if(MyGPSListener.currentLocation != null) {
        if(MyNetworkListener.currentLocation != null) {
            if(MyGPSListener.currentLocation.getAccuracy() <= MyNetworkListener.currentLocation.getAccuracy()) {
                useGPSLocation();

            }
            else if(MyGPSListener.currentLocation.getAccuracy() > MyNetworkListener.currentLocation.getAccuracy()) {
                useNetworkLocation();

            }
        }
        else {
            useGPSLocation();

        }

    }
    else if(MyNetworkListener.currentLocation != null){
        useNetworkLocation();

    }
kapa
  • 77,694
  • 21
  • 158
  • 175
Khawar Raza
  • 15,870
  • 24
  • 70
  • 127
  • why dont you try this http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-an/3145655#3145655 – change_is_necessity Sep 26 '11 at 14:52

3 Answers3

3

You should take a look at: http://developer.android.com/guide/topics/location/obtaining-user-location.html Especially the function: isBetterLocation

I would probably use something like the isBetterLocation. Since it's actually quite important to see how old the location updates are. At least if you really want to know where the user is know and not where the user were.

But you can surely implement a time check in your own function which you described.

David Olsson
  • 8,085
  • 3
  • 30
  • 38
  • Well first you need to include more of your code since I only see one small fraction. But when you get the location you could call isBetterLocation with the new location and the old one that you have saved. If it's better you use it, if not do not use the location. – David Olsson Sep 26 '11 at 15:29
1

You can use https://github.com/balwinderSingh1989/androidBestLocationTracker

Android Best Location Tracker is an Android library that helps you get user best location with a object named BaseLocationStrategy that would give a accurate location using accuracy alogrithm.

It has added support till latest android api levels and can fetch location in background with ease.

For usage, refer "READ ME" of project.

Balwinder SIngh
  • 1,831
  • 2
  • 24
  • 28
0

You can check Google's offical document. Define a model for the best performance

And Validate the accuracy of a location fix:

  1. Check if the location retrieved is significantly newer than the previous estimate.
  2. Check if the accuracy claimed by the location is better or worse than the previous estimate.
  3. Check which provider the new location is from and determine if you trust it more.

enter image description here

Mike Yang
  • 2,581
  • 3
  • 24
  • 27