1

我得到android手机的位置:

android.location.Location locationA;
            LocationManager locationManager;
            Criteria cri = new Criteria();
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            String tower = locationManager.getBestProvider(cri, false);
            locationA = locationManager.getLastKnownLocation(tower);
            if (locationA != null) {
                // lat = (double) (locationA.getLatitude() * 1E6);
                // longi = (double) (locationA.getLongitude() * 1E6);
                double lat = locationA.getLatitude();
                double longi = locationA.getLongitude();

                TextView txt = (TextView) findViewById(R.id.textView1);
                String td = String.valueOf(lat) + "," + String.valueOf(longi);
                txt.setText(td);
            }

为什么当我更改位置并再次获取当前位置时,Android手机的当前位置不会改变?

4

1 回答 1

1

使用 .检查您所在位置的时间locationA.getTime()。如果它不是最新的,请等待一个新的位置,然后停止。

private static Location currentLocation;
private static Location prevLocation;

public void yourMethod()
{
    locationManager.requestLocationUpdates(provider, MIN_TIME_REQUEST,
                            MIN_DISTANCE, locationListener);
}

private static LocationListener locationListener = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onLocationChanged(Location location) {
            gotLocation(location);
    }
};

private static void gotLocation(Location location) {
        prevLocation = currentLocation == null ?
                null : new Location(currentLocation);
        currentLocation = location;

        if (isLocationNew()) {
            // do something

            locationManager.removeUpdates(locationListener);
        }

}

private static boolean isLocationNew() {
    if (currentLocation == null) {
        return false;
    } else if (prevLocation == null) {
        return false;
    } else if (currentLocation.getTime() == prevLocation.getTime()) {
        return false;
    } else {
        return true;
    }
}
于 2012-09-10T08:50:53.453 回答