为CLPlacemark获取正确的缩放区域 [英] Get right zoom area for CLPlacemark

查看:164
本文介绍了为CLPlacemark获取正确的缩放区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MKLocalSearch 搜索某些地方,如城市中的城市或街道,以在 MKMapView



我像这样显示地标

  let loc = placemark 。位置! // CLLlacemark的CLLocation 
var mapRegion = MKCoordinateRegion()
mapRegion.center.longitude = loc.coordinate.longitude
mapRegion.center.latitude = loc.coordinate.latitude
mapRegion .span.latitudeDelta = 0.03 //我通过尝试
选择0.03 mapRegion.span.longitudeDelta = 0.03
mapView.setRegion(mapRegion,animated:true)

当地标是城市时,效果很好,因为它在合理的缩放级别显示更大的区域。但是当我想在城市中显示一条特定的街道(即 CLPlacemark 的位置)时。



现在我正在寻找一种方法,根据 CLPlacemark 的细节来计算右跨度(注意,你不知道类型有没有办法做到这一点?



首先,您需要检索适当的 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 。您可以使用以下解决方案:




I'm using MKLocalSearchto search for certain places like cities or streets in cities to shown them on an MKMapView

I show the placemark like this

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)

This works good when the place marks are cities as it shows a larger area at a reasonable zoom level. But when I want to show a specific street (which is the CLPlacemarks Location) in the city it is to far away.

Now I'm looking for a way to calculate the right span according to the "detail" of your CLPlacemark (Note that you don't know the type of the CLPlacemark upfront)

Is there a way to do this?

解决方案

Let me explain it fully.

First of all, you need to retrieve proper CLPlacemark objects.

If you would like to search for CLPlacemarks at some specific CLLocationCoordinate2D, then use this approach:

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;
 }];

Now that you got some proper CLPlacemark, you can use its .region property. Please note that documentation says that .region is CLRegion, but actually it is CLCircularRegion.

CLCircularRegion *theRegion = (id)thePlacemark.region;

However, MKMapView doesn't work with CLCircularRegion, while it works with MKMapRect. You can use the solution below:

How to convert CLCircularRegion to MKMapRect

MKMapRect theMapRect = [self rectForCLRegion:theRegion];

Now that we got our MKMapRect, we can pass it to our MKMapView like so:

[theMapView setVisibleMapRect:[theMapView mapRectThatFits:theMapRect]
                     animated:YES];

Or, if you would like to adjust the screen offsets a bit further, you can use:

[theMapView setVisibleMapRect:[theMapView mapRectThatFits:theMapRect]
                  edgePadding:UIEdgeInsetsMake(50, 50, 50, 50)
                     animated:YES];


Conclusion:

Code seems to work fine and adjusts the span automatically, using information provided via CLCircularRegion .radius property.

The picture below shows the result if you pass (37.382640, -122.151780)

To compare, this is the picture if you pass (37.382640, -12.151780)

这篇关于为CLPlacemark获取正确的缩放区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆