MKMapRectMake how to zoom out after setup

2.4k views Asked by At

I use MKMapRectMake to mark north east and south west to display a region. Here's how I do that:

routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
[self.mapView setVisibleMapRect:routeRect];

After I set up this display region, how can I zoom out the map a little? What is the best way to do this?

UPDATE

This is code that I use to get rect for setVisibleMapRect function:

for(Path* p in ar)
    {
        self.routeLine = nil;
        self.routeLineView = nil;

        // while we create the route points, we will also be calculating the bounding box of our route
        // so we can easily zoom in on it.
        MKMapPoint northEastPoint;
        MKMapPoint southWestPoint;

        // create a c array of points.
        MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * ar.count);

        for(int idx = 0; idx < ar.count; idx++)
        {
            Path *m_p = [ar objectAtIndex:idx];
            [NSCharacterSet characterSetWithCharactersInString:@","]];



            CLLocationDegrees latitude  = m_p.Latitude;
            CLLocationDegrees longitude = m_p.Longitude;

            // create our coordinate and add it to the correct spot in the array
            CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);


            MKMapPoint point = MKMapPointForCoordinate(coordinate);

            // adjust the bounding box
            // if it is the first point, just use them, since we have nothing to compare to yet.
            if (idx == 0) {
                northEastPoint = point;
                southWestPoint = point;
            }
            else
            {
                if (point.x > northEastPoint.x)
                    northEastPoint.x = point.x;
                if(point.y > northEastPoint.y)
                    northEastPoint.y = point.y;
                if (point.x < southWestPoint.x)
                    southWestPoint.x = point.x;
                if (point.y < southWestPoint.y)
                    southWestPoint.y = point.y;
            }

            pointArr[idx] = point;
        }

        // create the polyline based on the array of points.
        self.routeLine = [MKPolyline polylineWithPoints:pointArr count:ar.count];

        _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
        // clear the memory allocated earlier for the points
        free(pointArr);


        [self.mapView removeOverlays: self.mapView.overlays];
        // add the overlay to the map
        if (nil != self.routeLine) {
            [self.mapView addOverlay:self.routeLine];
        }

        // zoom in on the route.
        [self zoomInOnRoute];

    } 
4

There are 4 answers

0
AlexWien On

how can I zoom out the map a little?

Unfortunatley MkMapView setRegion behaves so strange that this does not work on iPhone. (ios 6.1.3) It works on iPad (ios 6.1.3)

setRegion and setVisibleMapRect

both changes the zoom factor only by steps of two. So you cannot programmatically zoom out by e.g 10%. Although Apple maps are vector based, they still snap the next higher zoom level that (would) fit the map tile pixels 1:1. Maybe to be compatible to map satellite display mode, which uses prerendered bitmaps.

Although bot methods should only correct the aspect ratio if you provided one of the lat/lon spans not perfectly, they additonally snap to said zoom levels.

Try it out: display map, then on button press: get the current region, make the longitude span 10% bigger (factor 1.1), set the region, then read it back, you will see on iphone simu and on iphone4 device the longitude span is now double the size, instead of factor 1.1.

Till today there exists no good solution.
Shame on you Apple.

2
Shekhar Gupta On

Try this: (Edit)

    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.01;
    span.longitudeDelta = 0.01;
    CLLocationCoordinate2D zoomLocation = newLocation.coordinate;
    region.center = zoomLocation;
    region.span = span;
    region = [mapViewObject regionThatFits:region];
    [mapViewObject setRegion:region animated:NO];
0
Ayush On

So in that case you need to find Centroid of a polygon and then pass that centroid values to this method so it would zoom to center of polygon i.e Centroid.

- (void)zoomMapView:(MKMapView *)mapview withLatitude:(Float32 )latitude andLongitude:(Float32 )longitude {
    MKCoordinateRegion region;
    region.span.latitudeDelta =0.005;  //Change values to zoom. lower the value to zoom in and vice-versa
    region.span.longitudeDelta = 0.009;//Change values to zoom. lower the value to zoom in and vice-versa
    CLLocationCoordinate2D location;
    location.latitude = latitude;   // Add your Current Latitude here.
    location.longitude = longitude; // Add your Current Longitude here.
    region.center = location;
    [mapview setRegion:region];
}

To use this method you need to pass three thing mapView, latitude and longitude i.e Position where to zoom.

0
Puneet Sharma On

You can use this custom function to center the Map around two points

- (void)centerMapAroundSourceAndDestination
{
  MKMapRect rect = MKMapRectNull;
  MKMapPoint sourcePoint = MKMapPointForCoordinate(southWestPoint);
  rect = MKMapRectUnion(rect, MKMapRectMake(sourcePoint.x, sourcePoint.y, 0, 0));
  MKMapPoint destinationPoint = MKMapPointForCoordinate(_northEastPoint);
  rect= MKMapRectUnion(rect, MKMapRectMake(destinationPoint.x, destinationPoint.y, 0, 0));
  MKCoordinateRegion region = MKCoordinateRegionForMapRect(rect);
  [_mapView setRegion:region animated:YES];
}