我的地图应该缩放到NSUserDefaults中保存的位置。虽然我的图钉出现在地图上,但是地图会进入海洋并且根本不会放大。这是我的代码。我做错了什么?
我确实定义了#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];
}
}
答案 0 :(得分:2)
以下是我的代码有效的示例,我希望它有所帮助(我认为你应该在你的代码中插入“MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];”)。如果您希望缩放设置为动画,请将代码移至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];