gpt4 book ai didi

objective-c - 确定两个坐标之间的中点

转载 作者:太空狗 更新时间:2023-10-30 03:12:20 25 4
gpt4 key购买 nike

我正在尝试确定 MKMapView 中两个位置之间的中点。我正在按照概述的方法 here (和 here )并在 Objective-C 中重写了它,但 map 的中心位于巴芬岛东北部某处,距离这两个点不远。

我的方法基于上面链接的java方法:

+(CLLocationCoordinate2D)findCenterPoint:(CLLocationCoordinate2D)_lo1 :(CLLocationCoordinate2D)_loc2 {
CLLocationCoordinate2D center;

double lon1 = _lo1.longitude * M_PI / 180;
double lon2 = _loc2.longitude * M_PI / 100;

double lat1 = _lo1.latitude * M_PI / 180;
double lat2 = _loc2.latitude * M_PI / 100;

double dLon = lon2 - lon1;

double x = cos(lat2) * cos(dLon);
double y = cos(lat2) * sin(dLon);

double lat3 = atan2( sin(lat1) + sin(lat2), sqrt((cos(lat1) + x) * (cos(lat1) + x) + y * y) );
double lon3 = lon1 + atan2(y, cos(lat1) + x);

center.latitude = lat3 * 180 / M_PI;
center.longitude = lon3 * 180 / M_PI;

return center;
}

2个参数有以下数据:

_loc1:
latitude = 45.4959839
longitude = -73.67826455

_loc2:
latitude = 45.482889
longitude = -73.57522299

以上内容已正确放置在 map 上(蒙特利尔及其周边地区)。我试图将 map 居中放置在 2 之间的中点,但我的方法返回以下内容:

latitude = 65.29055
longitude = -82.55425

它在北极的某个地方,但它应该在南边 500 英里左右。

最佳答案

如果有人需要 Swift 中的代码,我已经用 Swift 编写了库函数来计算多个坐标之间的中点:

//        /** Degrees to Radian **/
class func degreeToRadian(angle:CLLocationDegrees) -> CGFloat {
return ( (CGFloat(angle)) / 180.0 * CGFloat(M_PI) )
}

// /** Radians to Degrees **/
class func radianToDegree(radian:CGFloat) -> CLLocationDegrees {
return CLLocationDegrees( radian * CGFloat(180.0 / M_PI) )
}

class func middlePointOfListMarkers(listCoords: [CLLocationCoordinate2D]) -> CLLocationCoordinate2D {

var x = 0.0 as CGFloat
var y = 0.0 as CGFloat
var z = 0.0 as CGFloat

for coordinate in listCoords{
var lat:CGFloat = degreeToRadian(coordinate.latitude)
var lon:CGFloat = degreeToRadian(coordinate.longitude)
x = x + cos(lat) * cos(lon)
y = y + cos(lat) * sin(lon)
z = z + sin(lat)
}

x = x/CGFloat(listCoords.count)
y = y/CGFloat(listCoords.count)
z = z/CGFloat(listCoords.count)

var resultLon: CGFloat = atan2(y, x)
var resultHyp: CGFloat = sqrt(x*x+y*y)
var resultLat:CGFloat = atan2(z, resultHyp)

var newLat = radianToDegree(resultLat)
var newLon = radianToDegree(resultLon)
var result:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: newLat, longitude: newLon)

return result

}

详细答案可见here

针对 Swift 5 进行了更新

func geographicMidpoint(betweenCoordinates coordinates: [CLLocationCoordinate2D]) -> CLLocationCoordinate2D {

guard coordinates.count > 1 else {
return coordinates.first ?? // return the only coordinate
CLLocationCoordinate2D(latitude: 0, longitude: 0) // return null island if no coordinates were given
}

var x = Double(0)
var y = Double(0)
var z = Double(0)

for coordinate in coordinates {
let lat = coordinate.latitude.toRadians()
let lon = coordinate.longitude.toRadians()
x += cos(lat) * cos(lon)
y += cos(lat) * sin(lon)
z += sin(lat)
}

x /= Double(coordinates.count)
y /= Double(coordinates.count)
z /= Double(coordinates.count)

let lon = atan2(y, x)
let hyp = sqrt(x * x + y * y)
let lat = atan2(z, hyp)

return CLLocationCoordinate2D(latitude: lat.toDegrees(), longitude: lon.toDegrees())
}

关于objective-c - 确定两个坐标之间的中点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10559219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com