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

发布于 2025-01-05 06:42:22 字数 1670 浏览 3 评论 0原文

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

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

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

}

Why will my code not remove the map annotation?

I hit the button and it gets the users location and then pins it to the map. The problem is that when the button is hit a second, third, ....... time It just keeps adding pins instead of removing the first pin (simulating replacing the pin).

I have tried adding another button (clear pins) and following this thread :http://stackoverflow.com/questions/3027392/how-to-delete-all-annotations-on-a-mkmapview but the app just crashes with no debug info.

But in theory my code should work. So what am I not understanding?

- (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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望她远 2025-01-12 06:42:22

您要删除刚刚创建但尚未添加到地图中的注记:

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。

You're removing an annotation that you just created, and which hasn't been added to the map:

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

You can either locate the annotation on the map, and then remove it, or you can take a heavy-handed approach and simply remove all the annotations in one swoop. Since I don't know the UI interaction, I can't really outline how to locate the annotation for you. However, here's how to remove all:

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

HTH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文