单击按钮(创建注释)时删除地图注释
为什么我的代码不会删除地图注释?
我按下按钮,它会获取用户位置,然后将其固定到地图上。问题是,当按下按钮第二次、第三次……时,它只是不断添加引脚而不是删除第一个引脚(模拟更换引脚)。
我尝试添加另一个按钮(清除引脚)并遵循此线程: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要删除刚刚创建但尚未添加到地图中的注记:
您可以在地图上找到该注记,然后将其删除,也可以采取严厉的方法,简单地删除所有注记注释一举完成。由于我不了解 UI 交互,因此我无法真正概述如何为您找到注释。但是,以下是删除所有内容的方法:
HTH。
You're removing an annotation that you just created, and which hasn't been added to the map:
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:
HTH.