首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为CLPlacemark获取正确的缩放区域

为CLPlacemark获取正确的缩放区域
EN

Stack Overflow用户
提问于 2016-02-19 01:48:49
回答 1查看 557关注 0票数 4

我正在使用MKLocalSearch搜索某些地方,比如城市中的城市或街道,以便在MKMapView上显示它们

我像这样展示了广场

代码语言:javascript
复制
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前端的类型)。

有办法这样做吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-24 22:47:15

让我详细解释一下。

首先,您需要检索适当的CLPlacemark对象。

如果您想在某个特定的CLPlacemark上搜索CLLocationCoordinate2D,请使用以下方法:

代码语言:javascript
复制
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属性了。请注意,文档中说.regionCLRegion,但实际上是CLCircularRegion

代码语言:javascript
复制
CLCircularRegion *theRegion = (id)thePlacemark.region;

但是,MKMapView不适用于CLCircularRegion,而与MKMapRect工作。您可以使用以下解决方案:

How to convert CLCircularRegion to MKMapRect

代码语言:javascript
复制
MKMapRect theMapRect = [self rectForCLRegion:theRegion];

既然我们有了MKMapRect,我们就可以像这样把它传递给MKMapView了:

代码语言:javascript
复制
[theMapView setVisibleMapRect:[theMapView mapRectThatFits:theMapRect]
                     animated:YES];

或者,如果您想进一步调整屏幕偏移量,可以使用:

代码语言:javascript
复制
[theMapView setVisibleMapRect:[theMapView mapRectThatFits:theMapRect]
                  edgePadding:UIEdgeInsetsMake(50, 50, 50, 50)
                     animated:YES];

结论:

使用通过CLCircularRegion .radius属性提供的信息,代码似乎工作得很好,并自动调整跨度。

下图显示了如果您通过(37.382640,-122.151780)的结果。

要比较,这是图片,如果您通过(37.382640,-12.151780)

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35495900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档