Get accurate current location from Network Provider

早过忘川 提交于 2019-12-03 07:52:49

问题


I use the following code to get Current Location from a Network provider in my application:

LocationManager mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean network_enabled = mgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(network_enabled){
Location location = mgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

But it gives a location approximately 300-700 meters away from my actual location.

This is expected from a Network provider. But the problem is:

with only this Network provider enabled, and no GPS, I openned Foursquare application where it shows my current location exactly where I am. Now when I come back to my application, it shows the accurate current location or say the same location which Foursquare showed.

Same thing happens with Google apps like Navigator, Maps etc..,

How can this be done? How are other apps able to get the exact location, based on just the Network provider?

Complete Code:

public class MyLocationActivity extends Activity implements LocationListener {
    private LocationManager mgr;
    private String best;
    Location location;
    public static double myLocationLatitude;
    public static double myLocationLongitude;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        best = mgr.getBestProvider(criteria, true);
        location = mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        dumpLocation(location);
    }

    public void onLocationChanged(Location location) {

        dumpLocation(location);
    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

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

    }

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

    }

    @Override
    protected void onPause() {

        super.onPause();
        mgr.removeUpdates(this);
    }

    @Override
    protected void onResume() {

        super.onResume();
        mgr.requestLocationUpdates(best, 15000, 10, this);
    }

    private void dumpLocation(Location l) {

        if (l != null){

            myLocationLatitude = l.getLatitude();
            myLocationLongitude = l.getLongitude();
        }
    }
}

Thank You


回答1:


You are getting just the last known location. You should request location updates from the LocationManager. Use LocationManager.getLocationUpdates.

On your LocationListener on the onLocationChanged(Location location) method, you can check on the Location object, how accurate this location is, like this :

float accuracy=location.getAccuracy();

then, if this Location is accurate enough for you, you can stop receiving location updates from the LocationManager using removeUpdates() , and use the received Location. If the Location is not accurate enough, you can wait for a more precise Location, and stop the updates latter on.



来源:https://stackoverflow.com/questions/15634294/get-accurate-current-location-from-network-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!