If you want to use the actual annotation instead of the normal view located above the center of the map view, you can:
- use an annotation class with a custom coordinate property (a predefined
MKPointAnnotation class, for example). This avoids deleting and adding annotations when the center changes. - create annotation in viewDidLoad
- save the link to it in the property, say, centerAnnation
- update its coordinate (and name, etc.) in the map view
regionDidChangeAnimated delegate (make sure the map delegation property is specified)
Example:
@interface SomeViewController : UIViewController <MKMapViewDelegate> { MKPointAnnotation *centerAnnotation; } @property (nonatomic, retain) MKPointAnnotation *centerAnnotation; @end @implementation SomeViewController @synthesize centerAnnotation; - (void)viewDidLoad { [super viewDidLoad]; 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; [pa release]; } - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { centerAnnotation.coordinate = mapView.centerCoordinate; centerAnnotation.subtitle = [NSString stringWithFormat:@"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude]; } - (void)dealloc { [centerAnnotation release]; [super dealloc]; } @end
Now this will move the annotation, but not smoothly. If you want the annotation to move more smoothly, you can add the UIPanGestureRecognizer and UIPinchGestureRecognizer to the map view, as well as update the annotation in the gesture handler:
// (Also add UIGestureRecognizerDelegate to the interface.) // In viewDidLoad: UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; panGesture.delegate = self; [mapView addGestureRecognizer:panGesture]; [panGesture release]; UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; pinchGesture.delegate = self; [mapView addGestureRecognizer:pinchGesture]; [pinchGesture release]; - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { centerAnnotation.coordinate = mapView.centerCoordinate; centerAnnotation.subtitle = [NSString stringWithFormat:@"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude]; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { //let the map view and our gesture recognizers work at the same time... return YES; }
source share