我的应用程序中有一个正方形的MKMapView,我希望设置一个中心点和视图的精确高度/宽度(以米为单位)。
创建一个MKCoordinateRegion并将映射设置为它(如下面的代码所示)。
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center_coord, 1000.0, 1000.0);
[self.mapView setRegion:region animated:YES];..)不能正常工作,因为在这里使用区域只意味着至少会显示该区域,通常比区域显示的要多。
我计划使用setVisibleMapRect:动画:方法,因为我相信这将放大到实际通过的MKMapRect。
那么,有一种简单的方法可以在和MKMapRect之间进行转换吗?可能获取该区域的左上角和右下角坐标,并使用它们来生成MKMapRect?。
我在地图工具包函数参考里看不到任何有用的东西。
(使用iOS 5,Xcode 4.2)
发布于 2013-03-28 13:22:51
若要向堆中添加另一个实现,请执行以下操作:
- (MKMapRect)MKMapRectForCoordinateRegion:(MKCoordinateRegion)region
{
MKMapPoint a = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
region.center.latitude + region.span.latitudeDelta / 2,
region.center.longitude - region.span.longitudeDelta / 2));
MKMapPoint b = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
region.center.latitude - region.span.latitudeDelta / 2,
region.center.longitude + region.span.longitudeDelta / 2));
return MKMapRectMake(MIN(a.x,b.x), MIN(a.y,b.y), ABS(a.x-b.x), ABS(a.y-b.y));
}注:在MKMapRect和MKCoordinateRegion之间有很多种转换方式。这个当然不是MKCoordinateRegionMakeWithDistance()的精确逆,但它相当好地逼近它。所以,要小心来回转换,因为信息可能会丢失。
发布于 2016-02-10 17:11:11
这是Leo & Barnhart解决方案的一个快速版本。
func MKMapRectForCoordinateRegion(region:MKCoordinateRegion) -> MKMapRect {
let topLeft = CLLocationCoordinate2D(latitude: region.center.latitude + (region.span.latitudeDelta/2), longitude: region.center.longitude - (region.span.longitudeDelta/2))
let bottomRight = CLLocationCoordinate2D(latitude: region.center.latitude - (region.span.latitudeDelta/2), longitude: region.center.longitude + (region.span.longitudeDelta/2))
let a = MKMapPointForCoordinate(topLeft)
let b = MKMapPointForCoordinate(bottomRight)
return MKMapRect(origin: MKMapPoint(x:min(a.x,b.x), y:min(a.y,b.y)), size: MKMapSize(width: abs(a.x-b.x), height: abs(a.y-b.y)))
}发布于 2012-06-21 18:14:55
使用MKMapPointForCoordinate转换区域的2点(上/左和下/右),然后使用2 MKMapPoints创建MKMapRect
CLLocationCoordinate2D coordinateOrigin = CLLocationCoordinate2DMake(latitude, longitude);
CLLocationCoordinate2D coordinateMax = CLLocationCoordinate2DMake(latitude + cellSize, longitude + cellSize);
MKMapPoint upperLeft = MKMapPointForCoordinate(coordinateOrigin);
MKMapPoint lowerRight = MKMapPointForCoordinate(coordinateMax);
MKMapRect mapRect = MKMapRectMake(upperLeft.x,
upperLeft.y,
lowerRight.x - upperLeft.x,
lowerRight.y - upperLeft.y);https://stackoverflow.com/questions/9270268
复制相似问题