位置对象有另一个城市的lat lng

时间:2017-03-10 05:10:36

标签: android google-maps google-location-services

我正在尝试开发一个基于当前纬度和经度来计算行程距离的简单应用程序。

我遇到的基本问题是新纬度和经度有时远远超过3 - 2000公里。

有什么事情我做错了吗?请建议

这就是我正在做的......

 protected void startLocationUpdates() {
    if (mGoogleApiClient != null) {
        if (mGoogleApiClient.isConnected()) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission
                    .ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat

                            .checkSelfPermission(this, Manifest.permission
                                    .ACCESS_COARSE_LOCATION) !=
                            PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[]
                // permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the
                // documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, mLocationRequest, this);
        }
    } else {
        buildGoogleApiClient();
    }
}





 @Override
public void onLocationChanged(Location location) {
    if(oldLocation!=null)}{
    double oldLatitude = oldLocation.getLatitude(); 
    double oldLongitude = oldLocation.getLongitude();
    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();

    float[] f = new float[1];
     Location.distanceBetween(oldLatitude, oldLongitude, currentLatitude 
                            currentLongitude, f);
    }

1 个答案:

答案 0 :(得分:0)

尝试distanceTo方法。

<强> 实施例

@Override
public void onLocationChanged(Location location) {
    if(oldLocation!=null)}{
        float f = oldLocation.distanceTo(location);
    }