当wifi关闭时,distance To不起作用



你好,这是我遇到的一个非常奇怪的错误。我正试图将distanceTo()从我的位置移到一个固定的位置,我知道它有多远。当我的WiFi关闭时,我会得到一个非常奇怪的号码,但当WiFi打开时,即使它没有连接到任何网络,我也会得到正确的号码。这会是什么原因。我正在测试三星Galaxy S3。

locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
str = locationManager.getBestProvider(crit, false);
locationManager.requestLocationUpdates(str , 0, 1, this);
@Override
public void onLocationChanged(Location location) {
    LatLng myLatLng = new LatLng(location.getLatitude(), location.getLongitude());
    Location destination = new Location("dest");
    destination.setLatitude(54.008447);
    destination.setLongitude(-2.783383);
    float distance = location.distanceTo(destination);
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(myLatLng, 15);
    mMap.animateCamera(cameraUpdate);
    String destLocation;
    destLocation = distance/2 +"";
    Log.e("distance", destLocation);
    locationManager.removeUpdates(this);
}

我不使用getLastKnownLocation,因为我在某个地方读到它不可靠,最好在显示位置之前更新位置。

当您在地图上看到蓝点时,请使用mMap.getMyLocation()mMap.setOnMyLocationChangeListener(...),而不是所有使用LocationManager的代码。你会得到正确的位置。

最新更新