根据输入的地点坐标计算中心点

       在开发中,需要根据输入的地点坐标计算中心点,但是在百度,Google上搜索“根据输入的地点坐标计算中心点”或者“计算地图中心点”等等一系列关键字,折腾了很久,也没有找到任何解决方法。不过还好,最后在Google搜索“Latitude and longitude of the center”得到了解决方案,因为解决方案是在英文网站上找到的,所以将解决方案整理出来,供大家参考(呵呵,看来有些东西还是需要到国外去取取经)。

先给出地点坐标类的定义。

public class GeoCoordinate
{
    private readonly double latitude;
    private readonly double longitude;

    public double Latitude 
    { 
        get 
        { 
            return latitude; 
        } 
    }
    public double Longitude 
    { 
        get 
        { 
            return longitude;
        } 
    }

    public GeoCoordinate(double latitude, double longitude)
    {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public override string ToString()
    {
        return string.Format("{0},{1}", Latitude, Longitude);
    }
}

下面给出完整的计算方法。

/// <summary>
/// 根据输入的地点坐标计算中心点
/// </summary>
/// <param name="geoCoordinateList"></param>
/// <returns></returns>
public GeoCoordinate GetCenterPointFromListOfCoordinates(List<GeoCoordinate> geoCoordinateList)
{
    int total = geoCoordinateList.Count;
    double X = 0, Y = 0, Z = 0;
    foreach (GeoCoordinate g in geoCoordinateList)
    {
        double lat, lon, x, y, z;
        lat = g.Latitude * Math.PI / 180;
        lon = g.Longitude * Math.PI / 180;
        x = Math.Cos(lat) * Math.Cos(lon);
        y = Math.Cos(lat) * Math.Sin(lon);
        z = Math.Sin(lat);
        X += x;
        Y += y;
        Z += z;
    }
    X = X / total;
    Y = Y / total;
    Z = Z / total;
    double Lon = Math.Atan2(Y, X);
    double Hyp = Math.Sqrt(X * X + Y * Y);
    double Lat = Math.Atan2(Z, Hyp);
    return new GeoCoordinate(Lat * 180 / Math.PI, Lon * 180 / Math.PI);
}

对于400km以下时,可以采用下面的简化计算方法。

/// <summary>
/// 根据输入的地点坐标计算中心点(适用于400km以下的场合)
/// </summary>
/// <param name="geoCoordinateList"></param>
/// <returns></returns>
public GeoCoordinate GetCenterPointFromListOfCoordinates(List<GeoCoordinate> geoCoordinateList)
{
    //以下为简化方法(400km以内)
    int total = geoCoordinateList.Count;
    double lat = 0, lon = 0;
    foreach (GeoCoordinate g in geoCoordinateList)
    {
        lat += g.Latitude * Math.PI / 180;
        lon += g.Longitude * Math.PI / 180;
    }
    lat /= total;
    lon /= total;
    return new GeoCoordinate(lat * 180 / Math.PI, lon * 180 / Math.PI);
}

在以下页面包含有多种实现,大家可以参考。

http://stackoverflow.com/questions/6671183/calculate-the-center-point-of-multiple-latitude-longitude-coordinate-pairs

 详细的算法说明,可以参考。

http://www.geomidpoint.com/calculation.html

扩展阅读。

根据两点经纬度计算距离

  • 10
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
要根据多个经纬度坐标找出中心点,可以采用以下步骤: 1. 定义一个经度数组和纬度数组,分别存储输入经纬度坐标。 2. 计算经度的平均值和纬度的平均值,作为中心点经纬度坐标。 3. 将中心点经纬度坐标输出。 以下是一个示例代码: ```java import java.util.Scanner; public class CenterPoint { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入经纬度坐标个数:"); int n = scanner.nextInt(); double[] lng = new double[n]; // 经度数组 double[] lat = new double[n]; // 纬度数组 System.out.println("请输入经纬度坐标:"); for (int i = 0; i < n; i++) { System.out.print("第" + (i + 1) + "个经度:"); lng[i] = scanner.nextDouble(); System.out.print("第" + (i + 1) + "个纬度:"); lat[i] = scanner.nextDouble(); } double centerLng = getCenter(lng); // 中心点经度 double centerLat = getCenter(lat); // 中心点纬度 System.out.println("中心点经度:" + centerLng); System.out.println("中心点纬度:" + centerLat); } /** * 计算数组中的平均值 * * @param arr 数组 * @return 平均值 */ public static double getCenter(double[] arr) { double sum = 0; for (double num : arr) { sum += num; } return sum / arr.length; } } ``` 在上面的示例代码中,我们定义了 `getCenter` 方法来计算经度纬度的平均值,然后将其输出作为中心点经纬度坐标。在使用时,我们需要输入经纬度坐标个数以及每个坐标经纬度值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值