Search code examples
iosiphoneobjective-cmkmapviewmkannotation

iOS: is it possible to put an annotation at the center of the map and move the map while the annotation remains in the center at all times?


I don't know where to really begin, but I am wondering if it is possible to do this. I know how to put the annotation in mapview at the center.This is what I have so far. If anyone has any idea how to adjust the annotation to center of map when I change regions I will deeply appreciate it.

  - (IBAction)recenter:(id)sender {
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = mapView.centerCoordinate;
pa.title = @"Map Center";
pa.subtitle = [NSString stringWithFormat:@"%f, %f", pa.coordinate.latitude, pa.coordinate.longitude];
[mapView addAnnotation:pa];
self.centerAnnotation = pa;
  }


 - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
centerAnnotation.coordinate = self.mapView.centerCoordinate;
centerAnnotation.subtitle = [NSString stringWithFormat:@"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude];
   }

Solution

  • Instead of using an annotation, add a subview to the view that holds your map, and set the centers of the 2 views equal to each other. As the user pans the map, it'll look like the annotation is moving.

    Then whenever you need to find the coordinate that the center "annotation" is pointing to, you can use the centerCoordinate property of the map view.

    I've done this in map box and google maps, but not in MapKit, but I think it should work just the same.