【问题标题】:How to find the distance between geopoints?如何找到地理点之间的距离?
【发布时间】:2013-02-16 09:25:43
【问题描述】:

下午好。我有以下代码

           ParseQuery query = new ParseQuery("MyOb");
    query.findInBackground(new FindCallback() {
       public void done(List<ParseObject> myOb, ParseException e) {
         if (e == null) { 
            for ( i = 0; i < myOb.size(); i++) {

            geo1Dub = myOb.get(i).getParseGeoPoint("location").getLatitude();
                geo2Dub = myOb.get(i).getParseGeoPoint("location").getLongitude();
                geo1Int = (int) (geo1Dub*1E6);
            geo2Int = (int) (geo2Dub*1E6);
            pointGet = new GeoPoint(geo1Int, geo2Int);           

                    OverlayItem overlayitem = new OverlayItem(pointGet, title, title);               
            itemizedoverlay.addOverlay(overlayitem);                                 

                  } 

            mapOverlays.add(itemizedoverlay);
            mapView.postInvalidate();
          } else {


         }
       }
     });

从网站上,我正在获取点并将它们显示在地图上。 请告诉我如何使用

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

表明与我的观点。我的位置将在这一点上

 point = new GeoPoint(geoLat.intValue(), geoLng.intValue());

【问题讨论】:

    标签: android geopoints overlayitem


    【解决方案1】:

    两个地理点之间的距离:-

    public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
                Double EARTH_RADIUS = 6371.00; 
                double earthRadius = EARTH_RADIUS;
                double dLat = Math.toRadians(lat2-lat1);
                double dLng = Math.toRadians(lng2-lng1);
    
                double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                        Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLng/2) * Math.sin(dLng/2);
                double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
                double dist = earthRadius * c;
                return new Float(dist).floatValue();
            }
    

    【讨论】:

    • 来源?解释? (我知道,但您至少应该添加一个指向 wiki 的链接以供进一步研究)。
    【解决方案2】:

    GeoPoints创建Location对象,你就有distanceTO()方法。

    http://developer.android.com/reference/android/location/Location.html

    float distance[] = 0.0f;
    double lat = Double.valueOf(point.getLatitudeE6()/1E6);
    double lat = Double.valueOf(point.getLongitudeE6()/1E6);
    Location.distanceBetween(geo1Dub, geo2Dub, lat, lng, distance);
    

    您将在变量distance[0]中获得以米为单位的距离

    【讨论】:

    • 你能在我的代码中替换这个方法吗?我刚开始学习安卓
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多