简体   繁体   中英

Zoom to annotations not user location using Mapkit

I'm needing my application to zoom in on the annotations that are populated on the map, not the user's location. Is this a method that I would use in:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

or

-(void)viewWillAppear

So I'll also need the map to not only zoom in on the annotations, but also make sure that the annotations fit within the map's view. I've looked over a few examples, but I can't seem to find the answer that works for me. Any ideas?

Okay so I should have been more clear with my question, but basically I figured it out. So if you're wanting to add annotations to your map, and zoom to those annotations upon loading them then whether you're fetching JSON data from the web or defining the annotations upon initialization you need to do the following:

CLLocationCoordinate2D annotationCoordinate =
                CLLocationCoordinate2DMake([latDest doubleValue],
                                           [lngDest doubleValue]);

                Annotation *annotation = [[Annotation alloc] init];
                annotation.coordinate = annotationCoordinate;
                [self.mapView setCenterCoordinate:annotationCoordinate animated: YES];
                [self.mapView addAnnotation:annotation];

[self.mapView setCenterCoordinate:annotationCoordinate animated: YES];
[self.mapView addAnnotation:annotation];

I've created an annotation class that contains properties about each annotation that is gathered from the JSON data, but most importantly and most relevant to this question is the coordinate data which is exactly what we need. So I've defined my coordinate as:

annotationCoordiate

and lastly after adding the annotations to the map I've centered the map on the locations pulled from my data:

[self.mapView setCenterCoordinate:annotationCoordinate animated: YES];

Hopefully this made sense! Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM