Zooming Mkmapview to Fit Annotation Pins

Zooming MKMapView to fit annotation pins?

You've got it right.

Find your maximum and minimum latitudes and longitudes, apply some simple arithmetic, and use MKCoordinateRegionMake.

For iOS 7 and above, use showAnnotations:animated:, from MKMapView.h:

// Position the map such that the provided array of annotations are all visible to the fullest extent possible. 
- (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated NS_AVAILABLE(10_9, 7_0);

Zoom to fit current location and annotation on map

MKMapRectUnion computes and returns a new rect, nothing more. You need to tell the mapView to set its visible area to that new rect:

myMapView.setVisibleMapRect(zoomRect, animated: true)

Zoom MKMapView to fit polyline and annotation pins - Horizontal orientation

I had been working in your problem, for me this method [[self.map camera] setHeading:90.f]; works as intended, so maybe your problem is in the calculation on radians and the latter process, but maybe your problem is also related to the copy of camera. I use this method directly on the map´s camera and works great. I hope this helps you

Fitting annotations on a MKMapView while keeping user position centered

I found this solution to be the most reliable and @Anna suggested it as well so it might be an ok solution.

This is my method (implemented as a method of my inherited MKMapView

- (void)fitAnnotationsKeepingCenter {
CLLocation *centerLocation = [[CLLocation alloc]
initWithLatitude:self.centerCoordinate.latitude
longitude:self.centerCoordinate.longitude];

// starting distance (do not zoom less than this)
CLLocationDistance maxDistance = 350;

for (id vehicleAnnotation in [self annotations]) {
CLLocation *annotationLocation = [[CLLocation alloc] initWithLatitude:vehicleAnnotation.coordinate.latitude longitude:vehicleAnnotation.coordinate.longitude];
maxDistance = MAX(maxDistance, [centerLocation distanceFromLocation:annotationLocation]);
}

MKCoordinateRegion fittedRegion = MKCoordinateRegionMakeWithDistance(centerLocation.coordinate, maxDistance * 2, maxDistance * 2);
fittedRegion = [self regionThatFits:fittedRegion];
fittedRegion.span.latitudeDelta *= 1.2;
fittedRegion.span.longitudeDelta *= 1.2;

[self setRegion:fittedRegion animated:YES];
}

Trouble with MKMapView zoom to annotation on load

In your class declaration make these your class variables:

    let distanceSpan:CLLocationDegrees = 300
let redRocks:CLLocationCoordinate2D = CLLocationCoordinate2DMake(39.665496, -105.205438)

In ViewWillAppear, do this:

    override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animted)
view.layoutIfNeeded()

let redRocksPin = RedRocksPin(title: "Jerry Garcia 1/2/72", subtitle: "Red Rocks Amphitheatre", coordinate: redRocks)
mapView.addAnnotation(redRocksPin)
}

And then this:

    override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
mapView.setRegion(MKCoordinateRegionMakeWithDistance(redRocks, distanceSpan, distanceSpan), animated: true)
}

So what this does is in your viewwillAppear, it will draw the mapView, so you are able to add the annotation without causing a crash, then in the viewdidappear, it will just zoom in on the annotation you added.

Not sure if this is the best way, but something to try.

What's the best way to zoom out and fit all annotations in MapKit

-(void)zoomToFitMapAnnotations:(MKMapView*)mapView
{
if([mapView.annotations count] == 0)
return;

CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;

CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;

for(MapAnnotation* annotation in mapView.annotations)
{
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}

MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides

region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}

Taken from: http://codisllc.com/blog/zoom-mkmapview-to-fit-annotations/

(Use it all the time.)



Related Topics



Leave a reply



Submit