【问题标题】:Remove Map Annotation on Button Click (that created annotation)删除按钮单击时的地图注释(创建注释)
【发布时间】:2012-02-13 16:37:43
【问题描述】:

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

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

我尝试添加另一个按钮(清除引脚)并关注此线程: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];

}

【问题讨论】:

    标签: iphone ios mkmapview mkannotation


    【解决方案1】:

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

    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。

    【讨论】:

    • 非常感谢。我正在尝试“NSArray *annotations = [mapView annotations]; for (id annotation in annotations) { if ([annotation isKindOfClass:[MKUserLocation class]]) { continue; } [mapView removeAnnotation:annotation]; }” 但它一直在崩溃.这很好用
    • 听起来……您不想在使用数组时更改它。
    • 如果我想从数据库中删除 pin 记录,我们可以使用这个逻辑正确地从地图中删除 pin 吗?当我点击 pin 时,我得到了所有带有 pin_id 的记录,但我无法找到点击 pin 的 pin_id ?请你帮助摆脱这种情况。
    猜你喜欢
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2016-02-16
    • 2011-07-22
    • 1970-01-01
    相关资源
    最近更新 更多