0

我的地图应该缩放到保存在 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];

    }  

}
4

1 回答 1

2

这是我的代码示例,希望对您有所帮助(我认为您应该在代码中插入“MKCoordinateRegionadjustedRegion = [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];
于 2013-09-09T06:54:18.267 回答