Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

mapkit - display route on iOS 7 maps: addOverlay has no effect

i want display a point to point route inside my mapView, i use this code for create the route:

- (IBAction)backToYourCar {
    MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:self.annotationForCar.coordinate addressDictionary:nil];
    NSLog(@"coordiante : locationIniziale %f", sourcePlacemark.coordinate.latitude);
    MKMapItem *carPosition = [[MKMapItem alloc] initWithPlacemark:sourcePlacemark];
    MKMapItem *actualPosition = [MKMapItem mapItemForCurrentLocation];
    NSLog(@"coordiante : source %f, ActualPosition %f", carPosition.placemark.coordinate.latitude ,actualPosition.placemark.coordinate.latitude);
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    request.source = actualPosition;
    request.destination = carPosition;
    request.requestsAlternateRoutes = YES;

    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        if (error) {
            NSLog(@"Error : %@", error);
        }
        else {
            [self showDirections:response]; //response is provided by the CompletionHandler
        }
    }];
}

and this for show the route on the map:

- (void)showDirections:(MKDirectionsResponse *)response
{
    for (MKRoute *route in response.routes) {
        [self.mapView addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
    }
}

actually this code does nothing.

if i try to print the the distance of route i get the correct value:

route distance: 1910.000000

then the route is right, but i can't understand why it doesn't appear on the map!

Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

after a day of research i have solved with this 3 steps:

  1. Set the delegate (self.mapView.delegate = self).
  2. import the MKMapViewDelegate
  3. Implemente the new iOS7 MapView delegate method: - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay(id<MKOverlay>)overlay:

this is my implementation:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolyline *route = overlay;
        MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
        routeRenderer.strokeColor = [UIColor blueColor];
        return routeRenderer;
    }
    else return nil;
}

this method is automatically called by the delegate when you add the polyline on the map.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...