計算多個點的中心點座標
阿新 • • 發佈:2019-01-01
400Km以上時計算方式:
/// <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); }
/// <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://blog.csdn.net/yl2isoft/article/details/16368397
在以下頁面包含有多種實現,大家可以參考。
詳細的演算法說明,可以參考。
http://www.geomidpoint.com/calculation.html