使用 getLastKnownLocation() 获取位置无法获得正确的位置

Getting Location With getLastKnownLocation() does not get the right location

我正在开发处理 Google 地图 Api 的 Android 应用程序,我尝试使用 mMap.getLastKnownLocation(); 获取我的当前位置然后获取 latitudelongitude 之后'传递给将相机移动到我的位置的方法

这是获取我的位置的部分

    Criteria criteria = new Criteria();
  Location location = locationManager.getLastKnownLocation(locationManager
            .getBestProvider(criteria, false));

    if( location != null ){
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        AddMarkerAndMoveCamera(latitude,longitude);
        Toast.makeText(MapsActivity.this, "Latitude: " + latitude + "\nLongitude: " + longitude,Toast.LENGTH_LONG ).show();
    }

这是移动相机的部分

 public void  AddMarkerAndMoveCamera(double lat,double lng)
{
    LatLng MyLocation = new LatLng(lat, lng);
    mMap.addMarker(new MarkerOptions().position(MyLocation).title("This is Your Location ! Now !"));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(MyLocation,15));


}

这是我在应用程序运行时得到的。

如果有任何关于如何获得真实位置的想法,而不仅仅是 lastknownlocation ,我们将不胜感激。

Getting Location With getLastKnownLocation() does not get the right location

因为它正在获取此方法中传递的提供程序的最后已知位置。

您需要新位置。为此,check this link

编辑

在上面 link 中实现所需的东西后,你可以做这样的事情。

@Override
public void onLocationChanged(Location location) {
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    AddMarkerAndMoveCamera(latitude,longitude);
    Toast.makeText(MapsActivity.this, "Latitude: " + latitude + "\nLongitude: " + longitude,Toast.LENGTH_LONG ).show();
 }