iOS MapKit:代码重构,稍加调整

iOS MapKit:代码重构,稍加调整,ios,objective-c,mapkit,Ios,Objective C,Mapkit,使用下面的代码,当位置移动时,我会得到多个红色的位置标记。请看下面的图片 我想删除旧的Placemark并在当前位置用新的Placemark更新它。因此,在任何给定的时间,只有一个红色的地点标记 - (void)locationManager:(CLLocationManager *)aManager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { CLLocat

使用下面的代码,当位置移动时,我会得到多个红色的位置标记。请看下面的图片

我想删除旧的Placemark并在当前位置用新的Placemark更新它。因此,在任何给定的时间,只有一个红色的地点标记

- (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];
    }
  }];
}

在添加其他批注之前,请删除以前的批注

//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:

@界面中
声明如下属性:

@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;

在添加其他注释之前,请删除以前的注释

//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:

@界面中
声明如下属性:

@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;

可以将注释存储为属性:

在头文件
.h
@interface
中:

@property (nonatomic, strong) MapLocation *myAnnotation;
然后只需通过调用以下命令进行更新:

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

Apple文档:

您可以将注释存储为属性:

在头文件
.h
@interface
中:

@property (nonatomic, strong) MapLocation *myAnnotation;
然后只需通过调用以下命令进行更新:

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

Apple docs:

他可以只删除一个注释,以防跟踪位置时没有涉及到更多注释。是的,在这种情况下,他可以将最后一个注释存储为属性,并在添加新注释之前删除。感谢您的快速回复@沃特克鲁特科夫斯基是正确的。我只想删除最后一个注释。你能更新代码吗?谢谢,您所要做的就是实际更新注释。我认为更好更快的方法是更新它的变量,而不是删除然后重新添加@丹尼尔·伯德,你怎么看?我已经更新了我的答案。他可以集中精力只删除一个批注,以防跟踪位置时没有涉及更多批注。是的,在这种情况下,他可以将最后一个批注存储为属性,并在添加新批注之前将其删除。感谢您的快速回复@沃特克鲁特科夫斯基是正确的。我只想删除最后一个注释。你能更新代码吗?谢谢,您所要做的就是实际更新注释。我认为更好更快的方法是更新它的变量,而不是删除然后重新添加@丹尼尔·伯德,你怎么看?我已经更新了我的答案。