iOS MapKit:代码重构与小调整

问题描述:

随着下面的代码,当位置移动,我得到多个红色的地标。请看下面的图片。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]; 
    } 
    }]; 
} 

enter image description here

在您添加其他标注,移除先前药粥removeAnnotations:

如果你只想删除以前的注释,商店

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

相关的苹果文档链接它作为一个财产,并删除它呼吁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

他可以专注于消除的情况下只有一个标注有更多的注释不参与跟踪的位置。 – 2013-04-11 00:41:29

+0

是的,在这种情况下,他可以将最后一个注释存储为属性,并在添加新注释之前将其删除。 – danielbeard 2013-04-11 00:42:27

+0

感谢您的迅速回复。 @WojtekRutkowski是正确的。我只想删除最后一个注释。你能否更新代码?谢谢 – user1107173 2013-04-11 01:34:05

您可以将标注为物业:

在头文件

.h@interface

@property (nonatomic, strong) MapLocation *myAnnotation; 

然后,只需调用更新:

[myAnnotation setCoordinate:location.coordinate]; 
[myAnnotation setStreet:placemark.thoroughfare]; 
[myAnnotation setCity:placemark.locality]; 
... 

苹果文档:MKAnnotation/setCoordinate