1

我有一个应用程序,我在地图上放置了多个注释。但是现在我想在删除这些注释时跨越我的地图。这是我在地图上添加注释的代码。

- (void)viewDidLoad {

    [super viewDidLoad];
    mapView.mapType = MKMapTypeStandard;
    mapView.showsUserLocation = YES;
    NSMutableArray *annotations = [[NSMutableArray alloc]init];
    [annotations addObjectsFromArray:appDelegate.maparray];
    NSLog(@"%@",annotations);

    if ([annotations count])
    {
        for (int i =0; i < [annotations count]; i++) 
        {
             dict  = [annotations objectAtIndex:i];
            MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
            region.center.latitude = [[dict objectForKey:@"Latitude"] floatValue];
            region.center.longitude = [[dict objectForKey:@"Longitude"] floatValue];


            region.span.longitudeDelta = 0.6f;
            region.span.latitudeDelta = 0.6f;

            Title = [dict objectForKey:@"Title"];

            description = [dict objectForKey:@"DescriptionMobile"];
            NSLog(@"%@",description);
            NSURL *url = [NSURL URLWithString:[dict objectForKey:@"Image"]];
            NSData *data = [[NSData alloc] initWithContentsOfURL:url];
            image = [[UIImage alloc] initWithData:data];
            MyAnnotation *ann = [[MyAnnotation alloc] init];

            ann.title = Title;
            ann.subtitle = description;


            [mapView addAnnotation:ann];

        }
    }

}

如何将我的地图跨越到我的注释点,以便我的所有注释都可见并且我的地图也可以缩放。谢谢

4

2 回答 2

2

试试这个代码,它对我有用

- (void)zoomToFitMapAnnotations:(MKMapView *)mapView { 
    if ([mapView.annotations count] == 0) return; 

    CLLocationCoordinate2D topLeftCoord; 
    topLeftCoord.latitude = -90; 
    topLeftCoord.longitude = 180; 

    CLLocationCoordinate2D bottomRightCoord; 
    bottomRightCoord.latitude = 90; 
    bottomRightCoord.longitude = -180; 

    for(Annotation *annotation in mapView.annotations) { 
        topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); 
        topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); 
        bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); 
        bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); 
    } 

    MKCoordinateRegion region; 
    region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5; 
    region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;      
    region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; 

    // Add a little extra space on the sides 
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; 

    // Add a little extra space on the sides 
    region = [mapView regionThatFits:region]; 
    [mapView setRegion:region animated:YES]; 
}
于 2012-05-21T07:46:41.510 回答
0

我想你错过了这一行:

ann.coordinate = region.center;
于 2012-05-21T08:35:16.300 回答