0

I'm wanting to have an IBAction center my mapview's region on an annotation. The annotation is added to the mapview like this:

myAnnotation *pinLoc=[[myAnnotation alloc] init];
pinLoc.coordinate = mapview.centerCoordinate;
pinLoc.title = @"Title";
pinLoc.subtitle = @"subtitle";
[mapview addAnnotation:pinLoc];

I'd like to retreive it and move the region's center like this somehow:

- (IBAction)findPin:(id)sender {

    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };

    region.center.latitude = pinLoc.coordinate.latitude;
    region.center.longitude = pinloc.coordinate.longitude;
    region.span.latitudeDelta = 0.001f;
    region.span.longitudeDelta = 0.001f;
    [mapview setRegion:region animated:YES]; 
    [mapview setDelegate:self];
}

I am also looking for a way to tell if the pin is within a polygon. I've got that working using the user location, but again, I need to retrieve the pin's coordinates to get it working with the pin also.

Solid I
  • 580
  • 5
  • 13
  • 34

1 Answers1

1

The simplest way is to store a reference to the annotation when you add it.

For example, you could declare a retain property called pinToRemember of type myAnnotation and after adding the annotation, do this to save it for later:

[mapview addAnnotation:pinLoc];
self.pinToRemember = pinLoc;

Then in findPin:, you would do:

if (pinToRemember == nil)
    return;

region.center = pinToRemember.coordinate;


Alternatively, you could search through the map view's annotations array property and look for the annotation you are interested in by looking for its title or some other custom property. This could be done by using a simple for-loop.


You say you have the "way to tell if the pin is within a polygon" working but this answer may also be useful:
How to determine if an annotation is inside of MKPolygonView (iOS)

Community
  • 1
  • 1
  • Sometimes I can't see the answers that are right in front of me, thanks Anna. Your presence on StackOverflow has saved me countless times already in my early iOS Dev career. I actually am alrdy using your code to determine if a point is within a polygon! – Solid I Mar 20 '12 at 09:23