隨着下面的代碼,當位置移動,我得到多個紅色的地標。請看下面的圖片。iOS MapKit:代碼重構與小調整
我想刪除舊的地標並使用當前位置的新地標進行更新。所以在任何時候只會有一個紅色的地標。
- (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];
}
}];
}
他可以專注於消除的情況下只有一個標註有更多的註釋不參與跟蹤的位置。 – 2013-04-11 00:41:29
是的,在這種情況下,他可以將最後一個註釋存儲爲屬性,並在添加新註釋之前將其刪除。 – danielbeard 2013-04-11 00:42:27
感謝您的迅速回復。 @WojtekRutkowski是正確的。我只想刪除最後一個註釋。你能否更新代碼?謝謝 – user1107173 2013-04-11 01:34:05