Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to use an actual annotation instead of just a regular view positioned above the center of the map view, you can:</p> <ul> <li>use an annotation class with a settable coordinate property (pre-defined <code>MKPointAnnotation</code> class eg). This avoids having to remove and add the annotation when the center changes.</li> <li>create the annotation in viewDidLoad</li> <li>keep a reference to it in a property, say centerAnnotation</li> <li>update its coordinate (and title, etc) in the map view's <code>regionDidChangeAnimated</code> delegate method (make sure map view's delegate property is set)</li> </ul> <p>Example:</p> <pre><code>@interface SomeViewController : UIViewController &lt;MKMapViewDelegate&gt; { 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 </code></pre> <p>Now this will move the annotation but not smoothly. If you need the annotation to move more smoothly, you can add a <code>UIPanGestureRecognizer</code> and <code>UIPinchGestureRecognizer</code> to the map view and also update the annotation in the gesture handler:</p> <pre><code> // (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's and our gesture recognizers work at the same time... return YES; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload