Android 找到我的位置并计算距离

Android 找到我的位置并计算距离,android,gps,location,Android,Gps,Location,在我的设备上,一切正常,但其他设备显示出惊人的距离。它应该离点300米,显示6000公里 以下是我获取位置的方法: private SharedPreferences sPref; private LocationManager locationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState

在我的设备上,一切正常,但其他设备显示出惊人的距离。它应该离点300米,显示6000公里

以下是我获取位置的方法:

private SharedPreferences sPref;
      private LocationManager locationManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_layout);

        sPref=getSharedPreferences(ShareTag, MODE_PRIVATE);
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}

    @Override
      protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            1000 * 10, 10, locationListener);
        locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 1000 * 10, 10,
            locationListener);

      }
      private LocationListener locationListener = new LocationListener() {



            @Override
            public void onLocationChanged(Location location) {
              Log.d("MyLog", "my lat="+location.getLatitude()+" splash mylong="+location.getLongitude());
             saveCoordinates(location);
            }

            @Override
            public void onProviderDisabled(String provider) {

            }

            @Override
            public void onProviderEnabled(String provider) {
         //do nothing
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
              if (provider.equals(LocationManager.GPS_PROVIDER)) {
               //do nothing
              } else if (provider.equals(LocationManager.NETWORK_PROVIDER)) {
                   //do nothing
              }
            }
          };

          private void  saveCoordinates(Location location){
              Log.d("MyLog","get in the save coord");
              Editor ed=sPref.edit();
              ed.putString("myLatitude", ""+location.getLatitude());
              ed.putString("myLongtitude", ""+location.getLongitude());

              ed.commit();

          }
}
这就是我获取坐标并存储它们的方法

然后,当我需要距离的时候

sPref=context.getSharedPreferences(ShareTag, context.MODE_PRIVATE);
            myLatitude=Double.parseDouble(sPref.getString("myLatitude", "0"));
            myLongtitude=Double.parseDouble(sPref.getString("myLongtitude", "0"));
 float[] result = new float[1];
 latitude=address.getLatitude();
 longtitude=address.getLongitude();
Location.distanceBetween(latitude, longtitude, myLatitude, myLongtitude, result);
因此,结果[0]显示了不切实际的数字
latitude=address.getLatitude()
是正确的,所以问题在于myLatitude和MyLongtude。我无法查看这些设备的日志以查看用户的坐标是否错误。我的GPS工作不正常(中国设备),所以我从网络供应商那里得到了坐标——这可能就是为什么他们是对的

我能做什么?有什么建议吗

    for get current location ::-

    Criteria criteria = new Criteria();
                        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        String provider = locationManager.NETWORK_PROVIDER;
                        Location location = locationManager.getLastKnownLocation(provider);
                        if(location==null)
                        {

                        }else
                        {
                            longitude = location.getLongitude();
                            latitude = location.getLatitude();
                            Log.i("Latitude : ", String.valueOf(latitude));
                            Log.i("longitude : ", String.valueOf(longitude));
                        }



get distance in KM ::--

double distance = distFrom(latitude, longitude, latitude1, longitude1);

double km = distance / 0.62137;

method ::--

public static double distFrom(double lat1, double lng1, double lat2,
            double lng2) {
        double earthRadius = 3958.75;
        double dLat = Math.toRadians(lat2 - lat1);
        double dLng = Math.toRadians(lng2 - lng1);
        double sindLat = Math.sin(dLat / 2);
        double sindLng = Math.sin(dLng / 2);
        double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
                * Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2));
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double dist = earthRadius * c;

        return dist;
    }