3

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];

    } 
Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
1110
  • 7,829
  • 55
  • 176
  • 334
  • Did you Tried My response to your Question. – Ayush Aug 07 '13 at 07:24
  • Yes, but I don't need current lat/lng as you can see from updated question I draw polyline and make rect and I just want after this to make rect a bigger for few kilometers. – 1110 Aug 08 '13 at 11:28
  • I have Updated my answer have a look at it. – Ayush Aug 11 '13 at 16:33

4 Answers4

5

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];
Shekhar Gupta
  • 6,206
  • 3
  • 30
  • 50
  • I already draw route on map overlay in my question routeRect is MKMapRect object. I just want to change zoom level of that rect. – 1110 Jul 29 '13 at 12:16
  • I have combined something from all 3 answers and added solution to my question. – 1110 Jul 29 '13 at 12:49
2

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];
}
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
1

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.

Ayush
  • 3,989
  • 1
  • 26
  • 34
0

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.

AlexWien
  • 28,470
  • 6
  • 53
  • 83