tags:

views:

64

answers:

2

Hi,

I am usin location service for getting latitude and longitude of my location.While running my program it will give me wrong latitude and longitude.How can i get my current location details accurately? Is there any settings in mobile /device ?

String locationProvider = LocationManager.GPS_PROVIDER;
        if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER )) {
            locationProvider = LocationManager.NETWORK_PROVIDER;
        }
        Log.w(TAG,"CurrentLocProvider :"+locationProvider);

        location=locationManager.getLastKnownLocation(locationProvider);
        Log.i(TAG,"Location :"+location);
        if(location!=null){
            Log.i(TAG,"Location Lat :"+location.getLatitude());
            latitude = Double.toString(location.getLatitude());
            longitude = Double.toString(location.getLongitude());

            txtView.append("\nLatitude : "+latitude+"\n Longitude : "+longitude);
        }

After some time

public void onLocationChanged(Location location) {
Log.i(TAG,"Lat :"+location.getLatitude();
Log.i(TAG,"Long :"+location.getLongitude();


}

Also giving me wrong location.Some times USA,In another device china.In Different device it is giving different results? How can i get the currect result?

Thanks, aare.

A: 

Hm, it is very unlikely (but possible) to give you wrong coordinates - if the device GPS is not working properly.

BTW - the method getLastKnownLocation may return completely wrong data, as described in another stackoverflow question: this location can be "out-of-date", since the method returns the location when the GPS was used the last time.

Give a look at onStatusChanged, also. You may output the status changes of the GPS to get a better vision of what's going on.

Danail
A: 

You should also check if network location provider is enabled (users can have this disabled in settings):

locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

Or even better: use LocationManager.PASSIVE_PROVIDER.

Peter Knego
On emulator NETWORK_PROVIDER gives me Exception but on device it works but no use.
Give me sample on LocationManager.PASSIVE_PROVIDER. Please..
Peter Knego
location=locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
Peter Knego
PASSIVE_PROVIDER only works if some other application is using location information. This is useful for getting location updates on a regular basis without forcing use of GPS/network.
Peter Knego
Iam using Android level 4.LocationManager.PASSIVE_PROVIDER not included in this level