在安卓上找到起始地理点和结束地理点之间的中心地理点

5

我有起点纬度、经度和终点纬度、经度。

我使用纬度和经度找到地理点。

之后,我在两个地理点之间画了一条线。

我的问题是如何找到地理点的中心?

我在起点和终点位置上显示“OverlayItem”,但无法找到地理点的中心。

如何找到中心点?

我使用下面的代码在起点和终点位置上显示OverlayItem。

        drawable            = getResources().getDrawable(R.drawable.annot_start);
        point               = new GeoPoint((int)(startLatitude*1E6), (int)(startLongitude*1E6));
        overlayItem         = new OverlayItem(point, startAddress, "Welcome");
        itemizedOverlay[n]= new MyItemizedOverlay(drawable, mapView);
        itemizedOverlay[n].addOverlay(overlayItem);
        mapOverlays.add(itemizedOverlay[n]);

        drawable            = getResources().getDrawable(R.drawable.annot_end);
        point               = new GeoPoint((int)(endLatitude*1E6), (int)(endLongitude*1E6));
        overlayItem         = new OverlayItem(point, endAddress, "Welcome");
        itemizedOverlay[n+1]= new MyItemizedOverlay(drawable, mapView);
        itemizedOverlay[n+1].addOverlay(overlayItem);
        mapOverlays.add(itemizedOverlay[n+1]);
3个回答

9
你可以使用 Haversine公式 代码片段,并检查此 页面
public static void midPoint(double lat1,double lon1,double lat2,double lon2){

double dLon = Math.toRadians(lon2 - lon1);

//convert to radians
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
lon1 = Math.toRadians(lon1);

double Bx = Math.cos(lat2) * Math.cos(dLon);
double By = Math.cos(lat2) * Math.sin(dLon);
double lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) * (Math.cos(lat1) + Bx) + By * By));
double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);

//print out in degrees
System.out.println(Math.toDegrees(lat3) + " " + Math.toDegrees(lon3));
}

0

对我来说,检查过的解决方案不起作用。

无论如何,我已经找到了一个更简单的解决方案:

   private GeoPoint midPoint(double lat1,double lon1,double lat2,double lon2){
        //create origin geopoint from parameters
        GeoPoint origin = new GeoPoint(lat1,lon1);
        //create destination geopoints from parameters
        GeoPoint destination = new GeoPoint(lat2,lon2);
        //calculate and return center
        return GeoPoint.fromCenterBetween(origin, destination);
    }

如果您的起点和终点是GeoPoint,您可以直接使用"GeoPoint.fromCenterBetween"方法。


GeoPoint是从哪里来的?是Java还是C#? - Chad Grant

0

您可以通过取两点纬度的平均值和经度的平均值来近似中点。

如果您需要更精确的结果,可以使用 Haversine公式和一些代数来推导中点。


嗨@Trott,你能简要解释一下吗? - RVG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接