从MKCoordinateRegion MKMapView获取TopLeft和BottomRight

时间:2010-03-17 18:13:12

标签: objective-c mkmapview mkcoordinateregion mkcoordinatespan

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

我找到了this.

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

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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)

答案 2 :(得分:0)

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

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

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

(维基百科)