2013-04-11 35 views
0

隨着下面的代碼,當位置移動,我得到多個紅色的地標。請看下面的圖片。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

回答

2

在您添加其他標註,移除先前藥粥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

1

您可以將標註爲物業:

在頭文件

.h@interface

@property (nonatomic, strong) MapLocation *myAnnotation; 

然後,只需調用更新:

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

蘋果文檔:MKAnnotation/setCoordinate