IOS - MKMapView showAnnotations: animated: populated?

I want to be able to scale my MKMapView to fit its annotation. I was able to do this using the showAnnotations showAnnotations method. But I would also like to add some additions or inserts from the border of the map. This is because I have a translucent view that overlays the top of my map, and I don’t want the annotations to be places behind this view. I tried this:

 [self.mapView showAnnotations:annotations animated:YES]; [self.mapView setVisibleMapRect:self.mapView.visibleMapRect edgePadding:UIEdgeInsetsMake(100, 20, 10, 10) animated:NO]; 

But it does not work, as I would hope. Any ideas on how I can do this differently?

+10
ios objective-c cocoa-touch mkmapview
Feb 20 '14 at 15:33
source share
3 answers

You are doing it right. Try to change the registration, you will see the difference.

In another way, there should be something else in your code that prevents the view from changing.

EDIT: I was completely wrong. Try the following:

Create instance variable

 BOOL _mapNeedsPadding; 

and initialize it to NO;

Then set the mapView delegate to yourself and add it to the class header

Then add this to your class

 - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{ if(_mapNeedsPadding){ _mapNeedsPadding = NO; [self.mapView setVisibleMapRect:self.mapView.visibleMapRect edgePadding:UIEdgeInsetsMake(100, 20, 10, 10) animated:YES]; } } 

Finally, call the showAnnotations function as follows:

 _mapNeedsPadding = YES; [self.mapView showAnnotations:annotations animated:YES]; 

The showAnnimation function will call the regionDidChangeAnimated function. You need to set _mapNeedsPadding to NO after changing visibleMapRect, because this function (setVisibleMapRect: self) also triggers regionDidChangeAnimated.

Hope this helps!

+11
Feb 26 '14 at 13:30
source share

You can also just use

 [self.mapView showAnnotations:annotations animated:YES]; self.mapView.camera.altitude *= 1.4; 

to zoom out. Works well for me.

+15
Jul 20 '14 at 16:44
source share

Starting with iOS8, MKMapView has a layoutMargin property. When this is set, centerRegion: showAnnotations: and all methods that try to put the rectangle in the form of a map will take into account the above layout fields.

If your translucent view has a height of 40 points and is attached to the top of the map, the settings mapView.layoutMargin = UIEdgeInsetMake(40, 0, 0, 0) will do the magic.

If targeting iOS7, the map view uses the top and bottom layout of the manual of its containing controller to also bias its contents. Thus, you can override the controller's topLayoutGuide method to return the desired length.

 class ViewController: UIViewController { override var topLayoutGuide: UILayoutSupport { return MapLayoutGuide(length: 40) } } class MapLayoutGuide: NSObject, UILayoutSupport { var length: CGFloat init(length: CGFloat) { self.length = length super.init() } @available(iOS 9.0, *) var bottomAnchor: NSLayoutYAxisAnchor { return NSLayoutYAxisAnchor() } @available(iOS 9.0, *) var topAnchor: NSLayoutYAxisAnchor { return NSLayoutYAxisAnchor() } @available(iOS 9.0, *) var heightAnchor: NSLayoutDimension { return NSLayoutDimension() } } 
+10
Oct 19 '15 at 13:08 on
source share



All Articles