Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8640123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:12:09+00:00 2026-06-12T11:12:09+00:00

I’m trying to remove a pin from a map. I have an observer on

  • 0

I’m trying to remove a pin from a map. I have an observer on the @”selected” property of the MKPinAnnotationView so I know which object to delete. When the user taps the trash can icon and a pin is selected, this method gets called:

- (IBAction)deleteAnnotationView:(id)sender {
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[self.mapView viewForAnnotation:self.currentAddress];
    [pinView removeObserver:self forKeyPath:@"selected"];

    [self.mapView removeAnnotation:self.currentAddress];
    [self.map removeLocationsObject:self.currentAddress];
}

This method works fine if I do not drag the pin anywhere. If I drag the pin, my pinView in the above method returns nil, and the MKPinAnnotationView never gets removed from the MKMapView. I’m not sure why. Here’s the didChangeDragState delegate method:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
    if (newState == MKAnnotationViewDragStateEnding) {
        CLLocationCoordinate2D draggedCoordinate = view.annotation.coordinate;

        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        CLLocation *location = [[CLLocation alloc] initWithLatitude:draggedCoordinate.latitude longitude:draggedCoordinate.longitude];
        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
            // Check for returned placemarks
            if (placemarks && [placemarks count] > 0) {
                CLPlacemark *topResult = [placemarks objectAtIndex:0];

                AddressAnnotation *anAddress = [AddressAnnotation annotationWithPlacemark:topResult inContext:self.managedObjectContext];
                view.annotation = anAddress;
                self.currentAddress = anAddress;
            }
        }];
    }
}

In both the didChangeDragState: and deleteAnnotationView: methods, my self.address object has a valid address. For some reason though, when the pin is dragged, the pinView is nil. Any thoughts? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:12:10+00:00Added an answer on June 12, 2026 at 11:12 am

    Observing an annotation view’s selected property via KVO should be unnecessary since there’s the didSelectAnnotationView delegate method and (even better in your case) the selectedAnnotations property in MKMapView.

    Assuming the user taps the trash can after selecting a pin, the trash can tap method can get the currently selected annotation through the selectedAnnotations property. For example:

    if (mapView.selectedAnnotations.count == 0)
    {
        //No annotation currently selected
    }
    else
    {
        //The currently selected annotation is the first object in the array...
        id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0];
    
        //do something with ann...
    }
    

    In the above example, there was no need to access the annotation’s view, no observer and no need for your own “currentAddress” property.

    If instead you want to do some action immediately when an annotation is selected, you can put the code in the didSelectAnnotationView delegate method. There, the annotation selected is view.annotation.

    Regarding the issue on the drag-end, the current code is completely replacing the view’s annotation. I think this is only a good idea when the view is being created or re-used in the viewForAnnotation delegate method. In the drag-end method, you should instead try updating the view.annotation‘s properties instead of replacing with an entirely new object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to select an H1 element which is the second-child in its group
I have an array which has BIG numbers and small numbers in it. I
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I have a view passing on information from a database: def serve_article(request, id): served_article
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.