MKMapView centerCoordinate not exact - ios

When I try to place a pin in the center of the map using MKMapView's centerCoordinate it puts a pin roughly in the center, but it's several pixels south of the true center of the map.
I tried converting the map's center point to a coordinate
MKMapView.convertPoint(MKMapView.center) and that worked perfectly on ios8 but on ios7 is still off (now its south and west of the true center).
Anyone know how to determine how "off" the center coordinate is so I can adjust?
I need this because I am placing a crosshair image over the map and need the pin to appear in the center of the crosshair. I've verified with a ruler that the crosshair is centered on the map, it's the pin that is off.

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).

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.

Related

Bottom of Mapbox annotation marker pin does not sit on coordinates

I'm finding that any annotation image marker that is above some undefined size does not align the bottom of the marker to the coordinates. I've been unable to locate any documentation on it. Why is this and can it be fixed?
The coordinates are for the middle of Manhattan, New York City. Note how the green border sits well into the ocean. The coordinates are definitely right. When I use a different smaller image the annotation sits correctly and when I zoom in it sits correctly. But at anything above a 10.0 zoom level it does not sit correctly.
The coordinate will align with the center of the custom image. Since there currently isn't a way to specify a custom offset, you will need an image with its bottom 50% empty so that its center aligns with the coordinate on the map when placed.

MKMapView - to keep user at center and cover route drawn on mapview on the screen

I'm trying to achieve as follows:
User should always be at the center of the screen on MKMapView.
Route is drawn on the map as user will move.
I know, i can calculate the region to cover all the tracked points on the screen.
But here's my problem:
When i calculate the MKCoordinateRegion and setting it, it just fits the region that is best fitting to the screen but as soon as i'm trying to place user at center, a part of the line drawn on the MKMapView goes out of the screen.
Can anybody face this problem or any suggestions to handle this specific case, any help will be highly appreciated.
Thanks in advance.
I have accomplished it as follows:
Calculate the distance of farthest point from the user's current location (or any point you want to keep at the center).
Calculate the region, with your center point(user's current location in my case) and double the distance calculated above and make a region using te following code:
CLLocationCoordinate2D loc = [myLocation coordinate];
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance(loc, distance * 2, distance * 2);
Set the region on the MapView and the trail will be shown inside the screen keeping user's location at the center.
Thanks.

North of Map to point north pole always : programatically ios

The case is the replicate the north pole indicator into a button and perform the rotation.I know this can be done by rotating the map view entirely.Is there any other neat way where the annotation stays normal to the ipad orientation even after rotation
EDIT
as #AlexWain says
mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading
is an excellent solution ...but only possible when the user location displayed on map while rotating it
I need just to show a region and point the map towards north on the button click and sadly it is not the users current location,and is not in visible at that time
Apple has introduced that feature:
Use mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading
This turns the map in the direction you are looking or moving.
In that case north on the map matches the direction of north.
Update:
If the location is outside of the view, there is NO neat way to do it.
You have to rotate the view, and apply the inverse rotation to all elements which should not be rotated (e.g annotations, pins, copyright label)

MKMapView - Is there a way to use MKUserTrackingModeFollow with a modified center/focus?

I have a MapView that takes up the full screen and a few elements toward the top of the screen that overlay the map. These top elements potentially cover the blue location marker.
The map is currently tracking fine with MKUserTrackingModeFollow, but I'd like to move the tracking focus down about an inch (obviously the metric will vary by zoom level) to ensure the marker isn't covered. Is there a way to do this with MKUserTrackingModeFollow?
I've considered moving the map with the top elements, but due to design constraints this isn't an option.
Instead of using the userTrackingMode of the MKMapView object to follow the user's location, you could set up a delegate of CLLocationManager to receive location tracking events, and with each location update event you receive, set the center coordinate of the map view, with your vertical offset added to the coordinate value.
You can use the various Map Kit functions and MKMapView methods to convert your offset from some fraction of the map view's bounds.height to a MKMapPoint or CLLocationDistance value.

Adjust MkMapView Annotation

I'm adding annotations to an MkMapView and am using a custom image to do so. My custom image is box shaped with a little triangular arrow that's supposed to be right on the place that's being annotated. It seems like MapView by default annotates using the geometric center of the image. What's the best way to design around this problem? Manually moving the icon? Creating the icon in a specific way?
Here's the Apple docs for MKAnnotationView's centerOffset property:
By default, the center point of an annotation view is placed at the coordinate point of the associated annotation. You can use this property to reposition the annotation view as needed. This x and y offset values are measured in pixels. Positive offset values move the annotation view down and to the right, while negative values move it up and to the left.

Resources