删除按钮上的地图注释单击(创建注释)

时间:2012-02-13 16:37:44

标签: iphone ios mkmapview mkannotation

为什么我的代码不会删除地图注释?

我按下按钮,它获取用户位置,然后将其固定到地图上。问题是当按钮被击中第二,第三,......时间它只是不断添加引脚而不是移除第一个引脚(模拟更换引脚)。

我尝试添加另一个按钮(清除引脚)并遵循以下主题:http://stackoverflow.com/questions/3027392/how-to-delete-all-annotations-on-a-mkmapview但该应用只是崩溃了没有调试信息。

但理论上我的代码应该有效。那么我不理解的是什么?

- (IBAction)getLocation:(id)sender {
    MapAnnotation *ann1 =[[[MapAnnotation alloc] init]autorelease]; 

    // remove annotation
    [mapView removeAnnotation:ann1];


    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter=kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [locationManager startUpdatingLocation];

    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    MKCoordinateRegion region = {{0.0,0.0},{0.0,0.0}};
    region.center.latitude = locationManager.location.coordinate.latitude;
    region.center.longitude = locationManager.location.coordinate.longitude;
    region.span.longitudeDelta = 0.005f;
    region.span.latitudeDelta = 0.005f;
    [mapView setRegion:region animated:YES];
    [mapView setDelegate:sender];

    MKCoordinateRegion location1;
    location1.center.latitude =locationManager.location.coordinate.latitude;
    location1.center.longitude= locationManager.location.coordinate.longitude;
    location1.span.longitudeDelta=0.1;
    location1.span.latitudeDelta =0.1;


    // Add Annotation
    //MapAnnotation *ann1 =[[MapAnnotation alloc] init];
    ann1.title=@"You Parked Here";
    ann1.subtitle=@"";
    ann1.coordinate= location1.center;
    [mapView addAnnotation:ann1];

}

1 个答案:

答案 0 :(得分:3)

您正在删除刚刚创建的注释,而该注释尚未添加到地图中:

MapAnnotation *ann1 =[[[MapAnnotation alloc] init]autorelease];
// remove annotation
[mapView removeAnnotation:ann1];

您可以在地图上找到注释,然后将其删除,或者您可以采取严厉的方法,只需一次性删除所有注释。由于我不知道UI交互,我无法真正概述如何为您找到注释。但是,这是如何删除所有:

NSMutableArray *annotations = [NSMutableArray array];
for (id annotation in [mapView annotations])
{
  [annotations addObject:annotation];
}
[mapView removeAnnotations:annotations];

HTH。