gpt4 book ai didi

ios - 计算 map 正方形距 map 中心的坐标

转载 作者:行者123 更新时间:2023-11-28 23:36:04 25 4
gpt4 key购买 nike

我想获取矩形角的坐标。或者找到距离 map 中心 50 公里的最西北点的坐标。

有人知道我该怎么做吗?

关键是当我在 map 上移动时,我希望始终有一个矩形(矩形不需要绘制,我只需要它的坐标用于后端请求),它的角总是在距当前位置 50 公里处 map 的中心。

我正在考虑以某种方式使用 CLLocation 中的 distance 函数,但在这种情况下,我有距离,但没有坐标之一。

50km = mapCenterLocation.distance(from: coordinatesUnknown)

enter image description here

最佳答案

不太确定你的意思,但也许这可以帮助

func getNewTargetCoordinate(position: CLLocationCoordinate2D, userBearing: Float, distance: Float)-> CLLocationCoordinate2D{
//haversine formula
//r is earth radius
let r = 6378140.0
let latitude1 = position.latitude * (Double.pi/180);
let longitude1 = position.longitude * (Double.pi/180);
//bearing for user heading in degree
let brng = Double(userBearing) * (Double.pi/180);

//calculating user new position based on user distance and bearing can be seen at haversine formula
var latitude2 = asin(sin(latitude1)*cos(Double(distance)/r) + cos(latitude1)*sin(Double(distance)/r)*cos(brng));
var longitude2 = longitude1 + atan2(sin(brng)*sin(Double(distance)/r)*cos(latitude1),cos(Double(distance)/r)-sin(latitude1)*sin(latitude2));

//converting latitude as degree
latitude2 = latitude2 * (180/Double.pi)
longitude2 = longitude2 * (180/Double.pi)

// return location of user
return CLLocationCoordinate2DMake(latitude2, longitude2)
}

这项工作适用于 NE 方向和以米为单位的距离对于西北方向,我认为您可以将度数设置为 135,将距离设置为 5000。对于位置,您需要放置 map 中心位置。

编辑:对于自定义矩形,您可以先检查对角线度数

func getDiagonalDegree(x: Float, y:Float) -> Float{
return atan2(y,x)*(180/Double.pi)
}

现在您可以获得返回的对角线度数并将其放入 getNewTargetCoordinate 中。新方位角为270+对角线度数。

关于ios - 计算 map 正方形距 map 中心的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54869861/

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