2012-02-13 20 views
1

なぜ私のコードはマップアノテーションを削除しないのですか?ボタンをクリックしてアノテーションを削除ボタン(作成したアノテーション)

私はボタンを押して、ユーザーの場所を取得し、マップに固定します。問題は、ボタンが2番目、3番目、.......の時間に当たったときです。最初のピンを削除する代わりにピンを追加し続けるだけです(ピンの置換をシミュレート)。

私は別のボタン(クリアピン)を追加してこのスレッドに続いてみました:http://stackoverflow.com/questions/3027392/how-to-delete-all-annotations-on-a-mkmapviewデバッグ情報なしでクラッシュします。

しかし、理論的には私のコードはうまくいくはずです。では、私は何を理解していないのですか?

- (IBAction)getLocation:(id)sender { 
    MapAnnotation *ann1 =[[[MapAnnotation alloc] init]autorelease]; 

    // remove annotation 
    [mapView removeAnnotation:ann1]; 


    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.distanceFilter=kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; 
    [locationManager startUpdatingLocation]; 

    [mapView setMapType:MKMapTypeStandard]; 
    [mapView setZoomEnabled:YES]; 
    [mapView setScrollEnabled:YES]; 
    MKCoordinateRegion region = {{0.0,0.0},{0.0,0.0}}; 
    region.center.latitude = locationManager.location.coordinate.latitude; 
    region.center.longitude = locationManager.location.coordinate.longitude; 
    region.span.longitudeDelta = 0.005f; 
    region.span.latitudeDelta = 0.005f; 
    [mapView setRegion:region animated:YES]; 
    [mapView setDelegate:sender]; 

    MKCoordinateRegion location1; 
    location1.center.latitude =locationManager.location.coordinate.latitude; 
    location1.center.longitude= locationManager.location.coordinate.longitude; 
    location1.span.longitudeDelta=0.1; 
    location1.span.latitudeDelta =0.1; 


    // Add Annotation 
    //MapAnnotation *ann1 =[[MapAnnotation alloc] init]; 
    [email protected]"You Parked Here"; 
    [email protected]""; 
    ann1.coordinate= location1.center; 
    [mapView addAnnotation:ann1]; 

} 

答えて

3

あなたが作成した注釈を削除していて、どれがマップに追加されていない:あなたは、地図上の注釈を探し、それを削除するか

MapAnnotation *ann1 =[[[MapAnnotation alloc] init]autorelease]; 
// remove annotation 
[mapView removeAnnotation:ann1]; 

また、重大なアプローチを取って、すべてのアノテーションを1回のスワップで削除することもできます。私はUIのやりとりを知らないので、あなたのために注釈を見つける方法を本当に概説することはできません。ただし、すべてを削除する方法は次のとおりです。

NSMutableArray *annotations = [NSMutableArray array]; 
for (id annotation in [mapView annotations]) 
{ 
    [annotations addObject:annotation]; 
} 
[mapView removeAnnotations:annotations]; 

HTHです。

+0

ありがとうございました。 } [:注釈のMapView removeAnnotation]; { 続行: (注釈のIDアノテーション) { IF([MKUserLocationクラス]アノテーションisKindOfClass])のための、Iは[のMapView注釈]「にNSArray *注釈=をしようとしていました; } "しかし、それはクラッシュし続けました。これは素晴らしいです – Rick

+0

あなたはそれを使用している間、配列を変更したくない可能性があります。 – joshpaul

+0

データベースからピンレコードを削除したい場合、このロジックを使用して正しいピンからピンを削除できますか?ピンをクリックすると、pin_idですべてのレコードが取得されていますが、クリックされたピンのピンIDは見つかりませんでしたか?あなたはこの状況から出てくるのを助けてくれますか? –

関連する問題