I have a piece of code that determines both best region.span.latitudeDelta
and region.span.longitudeDelta
. It finds the top left and bottom right and takes the diff between lat-coordinates and long-coordinates.
The problem I have is that rendering actually differs than what I set when executing:
region = self.regionThatFits(region)
self.setRegion(region, animated: true)
As an example, if I first select the items I want to render (let's say from a table) and then switch to the map I get a different result (visually) than showing all points on the map and then selecting the ones I want to keep.
Even more, if I render all points on the map, then select a few and render selected, then remove filters and render all, the map is not the same as it was when I started - it shows all the points but it is smaller (looks zoomed out) in comparison to the initial view.
On debugging I can see that both scenarios generate the same span values but it is as if rendering ignores them. From debug using (the selected points in the UK):
Load all points:
region.span.latitudeDelta 7.439768
region.span.longitudeDelta 11.0
Filter points:
region.span.latitudeDelta 1.25
region.span.longitudeDelta 10.50477
Remove filter:
region.span.latitudeDelta 7.439768
region.span.longitudeDelta 11.0
I tried re-centering the map on a fixed coordinate before I re-render (instead of just on viewDidLoad) but it made no difference.
I would like to highlight that the map does not keep zooming out every time I run the above sequence. It kind of zooms out once and that's it.
Is there an obvious step that I am missing? A hidden Boolean that I need to set? Something else?
I also tried changing from regionThatFits to
regionToRender = MKCoordinateRegionMake(region.center, region.span)
And I tried the following too:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
self.mapView.region = mapView.region
}
No matter what I do, if I apply the annotation filter when I am already on the map it looks as if actual span is increased/doubled (or as if there is a forced frame around the map edges) making the map look as if it is zoomed out despite the debug output showing otherwise...
Below are the two images from the different scenarios. Any advice will be greatly appreciated.
Thanks!
Upon extensive debugging covering layouts, frames and bounds among other things, this is what is happening:
When the ViewController containing the map is loaded for the first time, the map does not actually fit the view. To be precise it is loaded in its free form which is effectively 600x492 WxH respectively. Height of 492 is simply:
After the first pass of rendering annotation (effectively in viewDidAppear), the MapView gets the correct height (on iphone 4s) of 372. This is simply:
As a result, any rendering from this point will result in a zoom-out like effect (making everything look smaller).
The solution is to calculate the size which the mapView will get and set in viewDidLoad. In addition, you should probably also set the mapView autoresizing mask to match the parent view:
I hope you find this helpful.