获得从网络提供准确的当前位置当前位置、准确、网络

2023-09-04 12:05:07 作者:流行的

我用下面的code,以获得当前位置从我的应用程序中的网络服务提供者:

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);

但它给出了一个位置距离我的实际位置约300-700米。

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

这是由网络服务提供者的预期。但问题是:

只有这个网络提供商启用,也没有GPS,我openned四方   应用它显示了我当前的位置正是我在哪里。现在   当我回到我的应用程序,它显示了精确的电流   位置或说哪四方表现出相同的位置。

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?

完成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();
        }
    }
}

感谢您

推荐答案

您越来越刚刚过去的已知位置。你应该要求从 LocationManager 位置更新。使用 LocationManager.getLocationUpdates

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

在您 LocationListener的 onLocationChanged(地点位置)方法,你可以检查的 位置 对象,如何准确的这个位置,这样的:

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();

那么,如果位置是给你足够精确,你可以停止接收来自 LocationManager 使用位置更新 removeUpdates() ,并使用所接收到的位置。如果位置不够准确,你可以等待更多的precise 位置,并停止更新后者上。

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.