I have a special case that I can't seem to wrap my head around.
I have a full screen mapview as a background. Of the lower half I have som other elements and I need to set the mapregion for my mapview, so that all annotations is visible and positioned in the upper half of the view.
I can find the maprect for all annotations. I can also find the rect for the upper half - even translate that into a maprect.
But how do I correlate these two maprects in order to move (and squeeze) the maprect-that-fits the annotations, into the upper half?
Can anybody give me a hint on this?
I found a rather simple but efficient solution for this.
The idea is to use a map rect instead of a region.
MKMapRect totalMapRect = MKMapRectNull;
for (id<MKAnnotation> annotation in self.mapView.annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect mapRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
totalMapRect = MKMapRectUnion(totalMapRect, mapRect);
}
[self.mapView setVisibleMapRect:totalMapRect edgePadding:UIEdgeInsetsMake(30, 30, 30 + verticalOffset, 30) animated:YES];
The idea is to iterate over all annotations and create a minimal map rect for each and union them together. This will give you the minimal rect for all annotations.
Then you can use the [setVisibleMapRect: edgePadding:]
and add any margin so that pins will be in screen and the lower margin should just be the margin + offset.
This works for multiple annotations, but for 1 annotation the rect will be so narrow that the map will be zoomed too much to make sense. In this case you'll have to fiddle around with the rect size.