【问题标题】:{Android App} How can I display distance from my current location and specified point?{Android App} 如何显示与当前位置和指定点的距离?
【发布时间】:2012-07-11 14:26:41
【问题描述】:

我已经研究了几个小时,我看到的唯一答案指向我 http://developer.android.com/reference/android/location/Location.html

并使用方法

public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] 结果)

我需要帮助来了解这与我的应用程序相关的工作原理。这就是我检索我的位置的方式。

    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);
  }

有人可以帮我理解一下如何将我当前的位置导入这个等式,然后在我的应用程序中显示距离吗? 谢谢你

【问题讨论】:

  • 首先您没有获得 当前 位置,请使用 requestSingleUpdate() 来执行此操作。其次,您正在计算当前位置与 .... 之间的距离?

标签: android gps distance


【解决方案1】:
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;

 }

如果您已经知道这两个位置的纬度和经度,则可以使用此代码。

【讨论】:

    【解决方案2】:

    创建位置监听器

    方法 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 >> 这个方法接受一个字符串并更新一个显示距离的文本视图。

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 2017-05-15
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      相关资源
      最近更新 更多