从MKCoordinateRegion MKMapView获取TopLeft和BottomRight

问题描述 投票:13回答:3

我检查了MKCoordinateRegionMKCoordinateSpanMKMapView文档中的属性,看看有没有办法从地图视图中获取TopLeft和BottomRight Lat Long,我没有找到任何。我知道跨度给了我Lat Lat delta但有没有办法从地图视图中获得实际的TopLeft和BottomRight lat long而不必进行奇怪的计算?

我找到了this.

不确定这是否足够准确。对此有何投票?

objective-c mkmapview mkcoordinateregion mkcoordinatespan
3个回答
27
投票

我不认为这些计算符合奇怪的条件:

CLLocationCoordinate2D center = region.center;
CLLocationCoordinate2D northWestCorner, southEastCorner;
northWestCorner.latitude  = center.latitude  + (region.span.latitudeDelta  / 2.0);
northWestCorner.longitude = center.longitude - (region.span.longitudeDelta / 2.0);
southEastCorner.latitude  = center.latitude  - (region.span.latitudeDelta  / 2.0);
southEastCorner.longitude = center.longitude + (region.span.longitudeDelta / 2.0);

4
投票

在Swift 3.0中作为扩展实现的简单计算:

extension MKCoordinateRegion {
    var northWest: CLLocationCoordinate2D {
        return CLLocationCoordinate2D(latitude: center.latitude + span.latitudeDelta  / 2,
                                      longitude: center.longitude - span.longitudeDelta / 2)
    }
    var northEast: CLLocationCoordinate2D {
        return CLLocationCoordinate2D(latitude: center.latitude + span.latitudeDelta  / 2,
                                      longitude: center.longitude + span.longitudeDelta / 2)
    }
    var southWest: CLLocationCoordinate2D {
        return CLLocationCoordinate2D(latitude: center.latitude - span.latitudeDelta  / 2,
                                      longitude: center.longitude - span.longitudeDelta / 2)
    }
    var southEast: CLLocationCoordinate2D {
        return CLLocationCoordinate2D(latitude: center.latitude - span.latitudeDelta  / 2,
                                      longitude: center.longitude + span.longitudeDelta / 2)
    }
}

用法:

var region: MKCoordinateRegion = ... some region here
print("North - West", region.northWest)

0
投票

你确定你有+ - 对吗?我没有得到有用的结果。当我切换+ - 时,我做到了。不过,我的代码可能在其他地方存在缺陷;)

经度为角度测量值,范围从Prime Meridian的0°到+ 180°向东和-180°向西。希腊字母λ(lambda),[3] [4]用于表示Prime Meridian东部或西部地球上的位置。

从技术上讲,纬度是以度数(以°标记)的角度测量值,范围从赤道(低纬度)0°到极点90°(北极为90°N或+ 90°,90°S或-90°) °为南极)。

(维基百科)

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