从MKCoordinateRegion MKMapView获取TopLeft和BottomRight

Dav*_*ave 13 objective-c mkcoordinateregion mkmapview mkcoordinatespan

我检查了文档中的属性MKCoordinateRegion,MKCoordinateSpanMKMapView看到有一种方法可以从地图视图中获取TopLeftBottomRight Lat Long,但我没有找到任何.我知道跨度给了我Lat Lat delta但是有没有办法从地图视图中获得实际的TopLeftBottomRight lat long而不必进行奇怪的计算?

我找到了这个.

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

Ole*_*ann 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);
Run Code Online (Sandbox Code Playgroud)

  • 如果该区域跨越国际日期线或极点,此代码是否会中断? (10认同)

Avt*_*Avt 6

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

var region: MKCoordinateRegion = ... some region here
print("North - West", region.northWest)
Run Code Online (Sandbox Code Playgroud)