4

I have a mapView in my (iOS7) app, where you can get directions from your current location to a fixed destination, by pressing a button. The app shows a polyline between the source and destination. Question is, how do i zoom the map to show the whole line?

- (IBAction)handleRoutePressed:(id)sender {
    self.routeButton.enabled = NO;
    self.routeDetailsButton.enabled = NO;

    // Make a directions request
    MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];
    // Start at our current location
    MKMapItem *source = [MKMapItem mapItemForCurrentLocation];
    [directionsRequest setSource:source];
    // Make the destination
    CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(57.0496683, 9.9220121);
    MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];
    MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
    [directionsRequest setDestination:destination];


    MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];


    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        // We're done
        //self.activityIndicator.hidden = YES;
        //[self.activityIndicator stopAnimating];
        self.routeButton.enabled = YES;

        // Now handle the result
        if (error) {
            NSLog(@"There was an error getting your directions");
            return;
        }

        // So there wasn't an error - let's plot those routes
        self.routeDetailsButton.enabled = YES;
        //self.routeDetailsButton.hidden = NO;
        _currentRoute = [response.routes firstObject];
        [self plotRouteOnMap:_currentRoute];
    }];
}

I have tried with

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(destinationCoords, 150*METERS_PER_MILE, 150*METERS_PER_MILE); //#define METERS_PER_MILE 1609.344
[_mapView setRegion:viewRegion animated:YES];

But naturally that gives me a fixed view every time.

catu
  • 888
  • 6
  • 24
  • 1
    If you want to fit a polyline, you could try one of the approaches listed here: http://stackoverflow.com/questions/13569327/zoom-mkmapview-to-fit-polyline-points. – Kamaros Nov 05 '13 at 23:14
  • 7
    Thank you. Solved it by adding `[_mapView setVisibleMapRect:[_routeOverlay boundingMapRect] edgePadding:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0) animated:YES];` – catu Nov 06 '13 at 07:44

0 Answers0