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 に答える