从地图中心计算地图方块的坐标

问题描述 投票:0回答:2

我想得到矩形角的坐标。或者找到距离地图中心50公里的最北点的坐标。

有谁知道我怎么做到这一点?

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

我正在考虑以某种方式使用distanceCLLocation函数,但在这种情况下我有距离,但不是其中一个坐标。

50km = mapCenterLocation.distance(from: coordinatesUnknown)

enter image description here

ios mapkit cllocation cllocationcoordinate2d
2个回答
1
投票

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

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。对于该位置,您需要放置地图中心位置。

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

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

所以现在你可以得到返回的对角线度,并把它放在getNewTargetCoordinate中。新轴承为270 +对角线。


0
投票

不确定我是否理解正确,但我认为这可能有所帮助,或者至少指出你的方向

CLLocationCoordinate2D northWest;

northWest = [mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:mapView];

有了这个,你将得到地图左上角的坐标,我想你只需要调整它来设置你的中心50公里的点并获得具有相同逻辑的坐标。

© www.soinside.com 2019 - 2024. All rights reserved.