Search code examples
iosobjective-cmkmapview

Why isn't the center of my mapview's frame lining up its center coordinate?


I'm trying to render a circle at the center of a mapview. I'm also displaying the user's current location on the mapview, and setting the center of the map to there. These two are not lining up, though.

Here is where I set up my circle:

[self.mapView setShowsUserLocation:YES];
CAShapeLayer *circle = [CAShapeLayer layer];
int radius = 50;
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
                                         cornerRadius:radius].CGPath;
// Set position of the circle.
// We use the frame of the mapview subtracted by circle radius to determine the center
circle.position = CGPointMake(CGRectGetMidX(self.mapView.frame)-radius,
                              CGRectGetMidY(self.mapView.frame)-radius);
circle.fillColor = [[UIColor alloc] initWithRed:0.5 green:0.5 blue:0.6 alpha:0.3].CGColor;
circle.strokeColor = [UIColor blackColor].CGColor;
circle.lineWidth = 1;

// Add to parent layer
[self.mapView.layer addSublayer:circle];

// Add 'crosshair'
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor blackColor];
view.frame = CGRectMake(0, 0, 5, 5);
[self.mapView addSubview:view];
view.center = self.mapView.center;

And in the didUpdateUserLocation delegate method I have:

    [self.mapView setCenterCoordinate:userLocation.coordinate];

Here's what it looks like on the simulator.

enter image description here

I eventually want to allow the user to pan and select a region of the map, but now I'm not sure if my centercoordinate of the map is even coinciding with the center of my circle. Any help? Thanks!


Solution

  • You may be running into the same bug I discovered recently. I have reported it to Apple and it was marked as a duplicate, so perhaps it'll be fixed in iOS 7.1.

    Bug with MKMapView's centerCoordinate during rotation?