在MKMapView上设置新的中心坐标后,注释不显示标注

时间:2011-09-28 12:31:35

标签: objective-c annotations mkmapview

我在我的应用程序中使用了一个显示一些注释的地图视图。

选择注释时,我尝试更改地图上的区域。选定的注释应该移动到中心。 如果注释位于中心附近,则在更改区域后会显示标注。 如果注释位于边框附近或者仅显示此注释的一部分,则区域将更改,但不会显示标注。

这是创建注释的代码:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

    MKAnnotationView *annView;
    annView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"currentloc"];
    if(annView == nil)
        annView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"]autorelease];

    if ([annotation isKindOfClass:[UserAnnotation class]]){
        [annView setImage:[UIImage imageNamed:@"marker_me.png"]];
    }

    if ([annotation isKindOfClass:[RestaurantAnnotation class]]){
        [annView setImage:[UIImage imageNamed:@"marker_resto.png"]];
        UIButton* rightButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];

        [rightButton setImage:[UIImage imageNamed:@"button_arrow_normal.png"] forState:UIControlStateNormal];
        [rightButton setImage:[UIImage imageNamed:@"button_arrow_active.png"] forState:UIControlStateHighlighted];

        annView.rightCalloutAccessoryView = rightButton;
    }

    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);

    return annView;
}

这是我选择注释时的代码:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{

    if ([view.annotation isKindOfClass:[RestaurantAnnotation class]]) {
        Restaurant *selectedRestaurant = [restaurants getRestaurantByName:view.annotation.title andAddress:view.annotation.subtitle];
        [selectedRestaurants addObject:selectedRestaurant];
        NSLog(@"Restaurant %@ selected",view.annotation.title); 

        CLLocationCoordinate2D coordinates;
        coordinates.latitude = [selectedRestaurant getLocationLat];
        coordinates.longitude = [selectedRestaurant getLocationLng];
        [mapView setCenterCoordinate:coordinates animated:YES];
    }
}

我需要添加更多内容来实现我的需求吗?

谢谢,抱歉英语不好。

0 个答案:

没有答案