Dav*_*ave 13 objective-c mkcoordinateregion mkmapview mkcoordinatespan
我检查了文档中的属性MKCoordinateRegion
,MKCoordinateSpan
并MKMapView
看到有一种方法可以从地图视图中获取TopLeft和BottomRight Lat Long,但我没有找到任何.我知道跨度给了我Lat Lat delta但是有没有办法从地图视图中获得实际的TopLeft和BottomRight 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)
在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)