4

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?

arnoapp
  • 2,416
  • 4
  • 38
  • 70
  • 1
    Have you checked this question https://stackoverflow.com/questions/9164920/updating-mkmapview-to-clplacemark-returned-from-clgeocoder ? – ljk321 Feb 21 '16 at 03:03
  • For me it is not clear what you want to do. Do you want to know why placemark for specific street in the city it is to far away? Or. How to calculate span according to the "detail" of the placemark? – Ramis Feb 22 '16 at 08:06
  • @Ramis I want to calculate the detail of the placemark – arnoapp Feb 22 '16 at 09:49
  • Code which you posted for me shown in the centered of the map with span. I can not reproduce or I do not understand where is problem. – Ramis Feb 23 '16 at 06:49
  • I want the right span for the detail of the placemark. If the placemark is a certain street in the city then I want to zoom closer as if it is just the city – arnoapp Feb 23 '16 at 10:48

1 Answers1

5

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)

enter image description here

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

enter image description here

Community
  • 1
  • 1
OlDor
  • 1,460
  • 12
  • 18
  • This is the perfect answer - you totally deserve the reward! Thanks for taking the time - on whatever I looked or searched for I couldn't figure it out – arnoapp Feb 24 '16 at 23:59