使用下面的代码,当位置移动时,我会得到多个红色地标。请看下面的图片。
我想删除旧的地标并在当前位置使用新的地标对其进行更新。因此,在任何给定时间,都只会有一个红色的地标。
- (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];
}
}];
}