Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom on specific MKMapView frame

My question is somehow related to this one. But with iOS7 I wonder if there is a simpler way to do it.

So basically I have a MKMapView working with a parallax effect, much like the Foursquare app.

enter image description here

So how can I zoom all the Annotations on the visible rect of the MKMapView?

Right now I have a working solution, that uses some values to offset the latitude of my annotations, but I find it very weird to do it.


Detail:

In order to give the map a parallax effect (when you are scrolling on the UITableView, the map is scrolling as well), you need to keep a part of the map bellow the table. In my case the frame of my MKMapView is (0 -120; 320 390) and the visible frame of it is (0 0, 320 150);. So the goal here is to be able to show the annotations (zoom in) on that visible frame.

like image 754
Rui Peres Avatar asked Nov 02 '22 06:11

Rui Peres


1 Answers

Something like this should work, but I have not actually tried the code:

MKAnnotation* firstAnnotaiton = annotations[0];

CLLocationDegrees north = firstAnnotation.coordinate.latitude;
CLLocationDegrees south = firstAnnotation.coordinate.latitude;
CLLocationDegrees east = firstAnnotation.coordinate.longitude;
CLLocationDegrees west = firstAnnotation.coordinate.longitude;

for (int i = 1; i < annotations.count; i++)
{
    MKAnnotation* annotation = annotations[i];

    if (north < firstAnnotation.coordinate.latitude)
        north = firstAnnotation.coordinate.latitude;
    if (south > firstAnnotation.coordinate.latitude)
        south = firstAnnotation.coordinate.latitude;
    if (west < firstAnnotation.coordinate.longitude)
        west = firstAnnotation.coordinate.longitude;
    if (east > firstAnnotation.coordinate.longitude)
        east = firstAnnotation.coordinate.longitude;
}

MKMapPoint upperLeft = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
    north,
    west
));
MKMapPoint lowerRight = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
    south,
    east
));

MKMapRect rect = MKMapRectMake(
    upperLeft.x,
    upperLeft.y,
    lowerRight.x - upperLeft.x,
    lowerRight.y - upperLeft.y
);

rect = [self.mapView mapRectThatFits:rect edgePadding:self.boundsInsets];

[self.mapView setVisibleMapRect:rect animated:animated];

The key here is to have self.boundsInsets be a UIEdgeInsets specifying the difference between the views actual size and its visible bounds.

like image 102
Stefan Fisk Avatar answered Nov 08 '22 08:11

Stefan Fisk