'Programmatically determine world bounds of Mapkit Mapview

I noticed a weird issue with iOS Mapkit's Mapview and was hoping someone had a solution.

When displaying the map there's a way to find the 'bounds' of the Apple MapView by using:

MKMapPoint(CLLocationCoordinate2D.init(latitude: 90, longitude: -180))
MKMapPoint(CLLocationCoordinate2D.init(latitude: -90, longitude: 180))

This will return something akin to: xMin = 0, xMax = 268435456 and yMin = 439674, yMax = 267995781

We can then use the visibleMapRect to figure out what part of the map we are seeing using the following parameters:

mapView.visibleMapRect.minX/maxX mapView.visibleMapRect.minY/maxY

However, I've noticed that on portrait mode on devices, there tends to be an offset from the top and bottom of the map. The offset becomes smaller as you zoom in. For example, if the map starts at y=439674, a fully zoomed out mapview on an iPhone will actually start at eg.: visibleMapRect.minY=539674 even if you scroll all the way to the top edge, zooming in one level and scrolling up again might get you to y=500000 etc., until the offset eventually disappears.

How can I dynamically calculate this offset? I want to account for this offset so I can properly project something on the map, as failing to account for it is causing some projection issues. (Mainly a parallax scrolling effect)

Notes:

  • I intentionally do not want to use the Mapkit Overlay renderer for this. I'm currently using a Metal view that draws graphics based on the position and size of the visibleMapRect.
  • I've also tried just using latitude and longitude coordinates to determine how to draw my graphics through metal (some shader stuff). This resulted in exactly the same parallax shifting effect. The reason I'm asking this question is because I'm convinced this detected 'offset' is causing the problem.
  • Most phones didn't have the problem on landscape mode. (iPad still did, however)
  • The problem only occurs on the y axis. The horizontal axis always projects perfectly fine.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source