0

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

我想删除旧的地标并在当前位置使用新的地标对其进行更新。因此,在任何给定时间,都只会有一个红色的地标。

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

在此处输入图像描述

4

2 回答 2

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

相关的苹果文档链接removeAnnotations:

如果您只想删除以前的注释,请将其存储为属性并通过调用将其删除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;
于 2013-04-11T00:37:31.650 回答
1

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

在头文件.h@interface

@property (nonatomic, strong) MapLocation *myAnnotation;

然后只需通过调用来更新它:

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

苹果文档:MKAnnotation/setCoordinate

于 2013-04-11T00:45:42.210 回答