让我完整地解释一下。
首先,您需要检索正确的CLPlacemark
对象。
如果您想搜索CLPlacemark
s 在某些特定的CLLocationCoordinate2D
,然后使用这种方法:
CLLocationCoordinate2D theCoordinate = CLLocationCoordinate2DMake(37.382640, -122.151780);
CLGeocoder *theGeocoder = [CLGeocoder new];
[theGeocoder reverseGeocodeLocation:[[CLLocation alloc] initWithLatitude:theCoordinate.latitude longitude:theCoordinate.longitude]
completionHandler:^(NSArray *placemarks, NSError *error)
{
CLPlacemark *thePlacemark = placemarks.firstObject;
}];
现在你已经得到了一些适当的CLPlacemark
,你可以使用它的.region
财产。请注意,文档说.region
is CLRegion
,但实际上是CLCircularRegion
.
CLCircularRegion *theRegion = (id)thePlacemark.region;
然而,MKMapView
不适用于CLCircularRegion
,同时它与MKMapRect
。您可以使用以下解决方案:
如何将 CLCircularRegion 转换为 MKMapRect https://stackoverflow.com/a/27284833/5165668
MKMapRect theMapRect = [self rectForCLRegion:theRegion];
现在我们得到了我们的MKMapRect
,我们可以将它传递给我们的MKMapView
像这样:
[theMapView setVisibleMapRect:[theMapView mapRectThatFits:theMapRect]
animated:YES];
或者,如果您想进一步调整屏幕偏移,您可以使用:
[theMapView setVisibleMapRect:[theMapView mapRectThatFits:theMapRect]
edgePadding:UIEdgeInsetsMake(50, 50, 50, 50)
animated:YES];
结论:
代码似乎工作正常,并使用通过提供的信息自动调整跨度CLCircularRegion
.radius
财产。
下图显示了如果通过(37.382640,-122.151780)的结果
为了比较,这是如果你通过(37.382640,-12.151780)的图片