MKMapView centerCoordinate not exact
Well I still don't know why in iOS7 MKMapView.convertPoint(MKMapView.center) differs from MKMapView.centerCoordinate, but did figure out why .centerCoordinate was off.
Part of the map was under the status bar and I ruled the center based on the visible part and overlaying images centered on the visible part, but the map computed center based on the whole map. So once I fixed the map not running under the status bar .centerCoordinate works and, on iOS7, works better than MKMapView.center.
In your answer you fixed one particular scenario where the center coordinate appears to be off, but really is not because it is just partially obscured by the status bar. Unfortunately, it is not as simple as that. There are other edge cases where the center coordinate actually can get offset a bit. (I notice it when I change frame of map view as keyboard is presented and later dismissed.) This is a curious little map view idiosyncrasy.
Anyone know how to determine how "off" the center coordinate is so I can adjust?
Yes, just convert the centerCoordinate
to a CGPoint
and compare to the center
. The only trick is to make sure you do these in the same coordinate system (in the example below, the coordinate system of the map view):
func adjustOverlayCenterYConstraint() {
let mapCenter = mapView.convert(mapView.centerCoordinate, toPointTo: mapView)
let viewCenter = mapView.superview!.convert(mapView.center, to: mapView)
overlayCenterYConstraint.constant = mapCenter.y - viewCenter.y
}
In this example, I have a centerY
constraint, whose constant
I am adjusting, but it illustrates the idea of how to programmatically determine whether the center is offset a bit and how to adjust it. Theoretically, you might have to adjust the x coordinate, too, but the idea would be the same.
You said:
is still off (now its south and west of the true center)
If it is off both south and west, then the problem is likely just the coordinate system. Remember, center
is in the coordinate system of the superview
, so make sure to convert to a consistent coordinate system (either get coordinateCenter
in coordinate system of map view’s superview
and compare to center
, or get both in the coordinate system of the mapview, like shown above).