Zooming out to fit all annotations in MapKit
Posted
by
Krismutt
on Stack Overflow
See other posts from Stack Overflow
or by Krismutt
Published on 2010-12-21T17:27:26Z
Indexed on
2010/12/21
18:54 UTC
Read the original article
Hit count: 344
Hey everybody!! I wanna zoom out so that all my annotations(my location and one more annotation) fit to the screen...what do I do wrong?? I get the following warning: "'getDistanceFrom:' is deprecated"....
-(void)viewDidLoad {
[super viewDidLoad];
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.showsUserLocation = TRUE;
mapView.delegate = self;
mapView.mapType = MKMapTypeStandard;
mapView.zoomEnabled = YES;
mapView.scrollEnabled = YES;
mapView.userInteractionEnabled = YES;
[mapView.userLocation setTitle:@"Nuvarande plats"];
[mapView.userLocation setSubtitle:@"Du är här"];
[self.view insertSubview:mapView atIndex:0];
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[mapView release];
}
-(void) locationManager: (CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog (@"Position uppdateras" );
location = newLocation.coordinate;
if (friZoom) {
MKCoordinateRegion region;
region.center.latitude = location.latitude;
region.center.longitude= location.longitude;
MKCoordinateSpan span;
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
region.span = span;
[mapView setRegion:region animated:TRUE];}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapview
viewForAnnotation:(id <MKAnnotation>)knappnal
{
if ([knappnal isKindOfClass:MKUserLocation.class])
{
return nil;
}
MKPinAnnotationView *knappnalView = (MKPinAnnotationView*)[mapview dequeueReusableAnnotationViewWithIdentifier:@"annot"];
if (!knappnalView)
{
knappnalView = [[[MKPinAnnotationView alloc] initWithAnnotation:knappnal reuseIdentifier:@"annot"] autorelease];
knappnalView.pinColor = MKPinAnnotationColorRed;
knappnalView.animatesDrop = YES;
knappnalView.canShowCallout = YES;
}
else {
knappnalView.annotation = knappnal;
}
return knappnalView;
}
- (IBAction)storeLocationInfo:(id) sender{
SparaPosition *position=[[SparaPosition alloc] initWithCoordinate:location];
[mapView addAnnotation:position];
savedPosition = location;
}
- (IBAction)visaPosition:(id)sender{
CLLocationCoordinate2D southWest =location;
CLLocationCoordinate2D northEast =savedPosition;
southWest.latitude = MIN(southWest.latitude, location.latitude);
southWest.longitude = MIN(southWest.longitude, location.longitude);
northEast.latitude = MAX(northEast.latitude, savedPosition.latitude);
northEast.longitude = MAX(northEast.longitude, savedPosition.longitude);
CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
CLLocationDistance meters = [locSouthWest getDistanceFrom:locNorthEast];
MKCoordinateRegion region;
region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0;
region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0;
region.span.latitudeDelta = meters / 111319.5;
region.span.longitudeDelta = 0.0;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
[locSouthWest release];
[locNorthEast release];
}
Would really appreciate an answer!
© Stack Overflow or respective owner