0

I've spent many hours trying to figure how to do this:

enter image description here

Having a placemark/annotation in the centerCoordinate of your mapView, when you scroll the map, the placemark should always stays in the center.

I've seen another app doing this too!

Marc
  • 11,341
  • 3
  • 27
  • 30
  • 1
    See [this question](http://stackoverflow.com/questions/6504536/how-to-add-annotation-on-center-of-map-view-in-iphone). –  Oct 19 '11 at 20:32
  • @Anna This seems to be the answer. Could you please post is as one so OP can accept it? – NGLN Oct 28 '11 at 20:39
  • @Anna can you help me out with this? – Jordan Brown Sep 04 '14 at 21:22
  • http://stackoverflow.com/questions/25577813/make-mkmapview-only-zoom-in-on-the-centercoordinate/25577949#25577949 – Jordan Brown Sep 04 '14 at 21:24
  • @JordanBrown, It's not clear what you're trying to achieve but I don't recommend the approach of using an actual MKAnnotation-based object. Instead, I suggest using a plain UIView positioned above the MKMapView. See http://stackoverflow.com/questions/20731589/tracking-mkmapview-centercoordinate-while-panning. –  Sep 04 '14 at 21:45

1 Answers1

1

Found my question in How to add annotation on center of map view in iPhone?

There's the answer :

If you want to use an actual annotation instead of just a regular view positioned above the center of the map view, you can:

  • use an annotation class with a settable coordinate property (pre-defined MKPointAnnotation class eg). This avoids having to remove and add the annotation when the center changes.
  • create the annotation in viewDidLoad
  • keep a reference to it in a property, say centerAnnotation
  • update its coordinate (and title, etc) in the map view's regionDidChangeAnimated delegate method (make sure map view's delegate property is set)

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 need the annotation to move more smoothly, you can add a UIPanGestureRecognizer and UIPinchGestureRecognizer to the map view and also 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's and our gesture recognizers work at the same time...
    return YES;
}
Community
  • 1
  • 1
Marc
  • 11,341
  • 3
  • 27
  • 30