我正在使用MKLocalSearch搜索某些地方,比如城市中的城市或街道,以便在MKMapView上显示它们
我像这样展示了广场
let loc = placemark.location! //CLLocation of CLPlacemark
var mapRegion = MKCoordinateRegion()
mapRegion.center.longitude = loc.coordinate.longitude
mapRegion.center.latitude = loc.coordinate.latitude
mapRegion.span.latitudeDelta = 0.03 // I choose 0.03 by trying
mapRegion.span.longitudeDelta = 0.03
mapView.setRegion(mapRegion, animated: true)当地标是城市时,这样做很好,因为它在合理的缩放水平上显示了更大的面积。但是,当我想显示一个特定的街道(这是CLPlacemark的位置)在城市,它是遥远的。
现在,我正在寻找一种根据CLPlacemark的“细节”计算正确跨度的方法(注意,您不知道CLPlacemark前端的类型)。
有办法这样做吗?
发布于 2016-02-24 22:47:15
让我详细解释一下。
首先,您需要检索适当的CLPlacemark对象。
如果您想在某个特定的CLPlacemark上搜索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是CLRegion,但实际上是CLCircularRegion。
CLCircularRegion *theRegion = (id)thePlacemark.region;但是,MKMapView不适用于CLCircularRegion,而与MKMapRect工作。您可以使用以下解决方案:
How to convert CLCircularRegion to MKMapRect
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)

https://stackoverflow.com/questions/35495900
复制相似问题