使用下面的代码,当位置移动时,我会得到多个红色地标。请看下面的图片。
我想删除旧的地标,并在当前位置使用新地标进行更新。因此,在任何给定时间,只会有一个红色地标。
- (void)locationManager:(CLLocationManager *)aManager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D regionCenter = newLocation.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(regionCenter, 400, 400);
[mapView setRegion:region animated:TRUE];
[self reverseGeocode:newLocation];
}
-(void)reverseGeocode:(CLLocation *)location
{
if (!geocoder)
geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* placemarks, NSError* error){
if (nil != error) {
UIAlertView *alert =
[[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Error translating coordinates into location",
@"Error translating coordinates into location")
message:NSLocalizedString(@"Geocoder did not recognize coordinates",
@"Geocoder did not recognize coordinates")
delegate:self
cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
otherButtonTitles:nil];
[alert show];
}
else if ([placemarks count] > 0) {
placemark = [placemarks objectAtIndex:0];
MapLocation *annotation = [[MapLocation alloc]init];
annotation.street = placemark.thoroughfare;
annotation.city = placemark.locality;
annotation.state = placemark.administrativeArea;
annotation.zip = placemark.postalCode;
annotation.coordinate = location.coordinate;
[self.mapView addAnnotation:annotation];
}
}];
}
答案 0 :(得分:2)
在添加其他注释之前,请删除之前的注释
//remove all previous annotations
NSArray *previousAnnotations = self.mapView.annotations;
[self.mapView removeAnnotations: previousAnnotations];
//then add your new annotation here
placemark = [placemarks objectAtIndex:0];
MapLocation *annotation = [[MapLocation alloc]init];
annotation.street = placemark.thoroughfare;
annotation.city = placemark.locality;
annotation.state = placemark.administrativeArea;
annotation.zip = placemark.postalCode;
annotation.coordinate = location.coordinate;
[self.mapView addAnnotation:annotation];
的相关Apple文档链接
如果您只想删除以前的注释,请将其存储为属性,并通过调用removeAnnotation:
在@interface
声明一个这样的属性:
@property (nonatomic, strong) MapLocation *previousAnnotation;
然后在你的代码中:
//remove the previous annotation if it exists
[self.mapView removeAnnotation: self.previousAnnotation];
//then add your new annotation here
placemark = [placemarks objectAtIndex:0];
MapLocation *annotation = [[MapLocation alloc]init];
annotation.street = placemark.thoroughfare;
annotation.city = placemark.locality;
annotation.state = placemark.administrativeArea;
annotation.zip = placemark.postalCode;
annotation.coordinate = location.coordinate;
[self.mapView addAnnotation:annotation];
//set your property
self.previousAnnotation = annotation;
答案 1 :(得分:1)
您可以将注释存储为属性:
标头文件.h
或@interface
中的:
@property (nonatomic, strong) MapLocation *myAnnotation;
然后通过调用:
更新它[myAnnotation setCoordinate:location.coordinate];
[myAnnotation setStreet:placemark.thoroughfare];
[myAnnotation setCity:placemark.locality];
...
Apple文档:MKAnnotation/setCoordinate