Reputation: 585
My map is supposed to zoom to the location saved in NSUserDefaults. Although my pin shows up on the map, the map goes to the ocean and does not zoom at all. Here is my code. What am I doing wrong?
I did define #define METERS_PER_MILE 1609.344
-(void)viewWillAppear:(BOOL)animated{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
if ([ud boolForKey:@"headquarters_coordinates"])
{
CLLocationCoordinate2D savedCoordinate;
savedCoordinate.latitude = [ud doubleForKey:@"headquarters_latitude"];
savedCoordinate.longitude = [ud doubleForKey:@"headquarters_longitude"];
//create annotation object using savedCoordinate and add to map view...
NSLog(@"Your HQ is at coordinates %f and %f",savedCoordinate.latitude, savedCoordinate.longitude);
MapViewAnnotation *ann1 =[[MapViewAnnotation alloc] init];
ann1.title=@"HQ";
ann1.subtitle=@"";
ann1.coordinate= savedCoordinate;
[mapView addAnnotation:ann1];
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(savedCoordinate, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
[mapView setRegion:viewRegion animated:YES];
}
}
Upvotes: 0
Views: 454
Reputation: 1099
Here is the example of my code that works, I hope it helps (I think you should insert "MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];" to your code). And if you want your zoom to be animated, move your code to viewdidapear.
#define METERS_PER_MILE 1609.344
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude =[mylatitude floatValue];
zoomLocation.longitude = [mylongitude floatValue];
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.1*METERS_PER_MILE, 0.1*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [mapViewElement regionThatFits:viewRegion];
[mapViewElement setRegion:adjustedRegion animated:YES];
Upvotes: 2