{Android App}如何显示距当前位置和指定点的距离?

我已经研究了几个小时,我看到的唯一答案指向我 http://developer.android.com/reference/android/location/Location.html 并使用该方法 public static void distanceBetween(double startLatitude,double startLongitude,double endLatitude,double endLongitude,float [] results) 我需要帮助了解这与我的应用程序有什么关系。这就是我如何检索我的位置。
    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    String provider = LocationManager.GPS_PROVIDER;
    Location location = locationManager.getLastKnownLocation(provider);

    updateWithNewLocation(location);
  }

  private void updateWithNewLocation(Location location) {
    String latLongString;
    TextView myLocationText; 
    myLocationText = (TextView)findViewById(R.id.myLocationText);
    if (location != null) {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      latLongString = "Lat:" + lat + "nLong:" + lng;
    } else {
      latLongString = "No location found"; 
    }
    myLocationText.setText("Your Current Position is:n" + 
                           latLongString);
  }
有人可以帮我解决一下如何将我当前的位置导入这个等式,然后在我的应用程序中显示距离吗? 谢谢     
已邀请:
public double calcdist()
            {
                int MILLION = 1000000;
                int EARTH_RADIUS_KM = 6371;

                double lat1 = la1 / MILLION;// latitude of location 1
            double lon1 = lo1 / MILLION; //longitude of location 1
            double lat2 = la2 / MILLION; //latitude of location 2
            double lon2 = lo2 / MILLION;//longitude of location 2

            double lat1Rad = Math.toRadians(lat1);
            double lat2Rad = Math.toRadians(lat2);
            double deltaLonRad = Math.toRadians(lon2 - lon1);

            double dist = Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad) * Math.cos(lat2Rad)
                    * Math.cos(deltaLonRad))
                    * EARTH_RADIUS_KM;
            return dist;

 }
如果您已经获得了两个位置的纬度和经度,则可以使用此代码。     
创建位置监听器 方法isAccurateForUse是一种自定义方法,可以简单地检查位置的准确性。 这应该工作:
public boolean locChanged = false;
public LocationManager locMgr = null;
public Location referenceL = null; // Your reference location
public Location currentKL = null; // Current known location
public Location currentLKL = null; // Current last known location

//Initialise the GPS listener
locMgr = (LocationManager) appCtx.getSystemService(Context.LOCATION_SERVICE);
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, fixGpsListener);
currentLKL = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

//Define the GPS listener
LocationListener fixGpsListener = new LocationListener()
{
    public void onLocationChanged(Location location)
    {
        locChanged = true;
        if (currentKL == null)
            currentKL = location;
        else if (isAccurateForUse())
        {
            currentLKL = currentKL;
            currentKL = location;
            updateDistance(meterToKilometer(currentKL.distanceTo(referenceL)));
        }
    }

    public void onProviderDisabled(String provider)
    {}

    public void onProviderEnabled(String provider)
    {}

    public void onStatusChanged(String provider, int status, Bundle extras)
    {}
};
currentKL.distanceTo(referenceL)>>给出以米为单位的距离 meterToKilometer >>将米转换为千米 updateDistance >>此方法接受一个字符串并更新显示距离的文本视图。 干杯     

要回复问题请先登录注册