0

This is the code i have to draw directions to a location just as the normal iPhone maps application.

-(void)initUserCoords{


    //NSLog(@"This if from new %@ %@", self.latitude, self.longitude);



    double dLat = 59.2462;
    double dLong = 15.1679;

    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(dLat, dLong) addressDictionary:nil];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];

    request.source = [MKMapItem mapItemForCurrentLocation];

    request.destination = mapItem;
    request.requestsAlternateRoutes = YES;
    MKDirections *directions =
    [[MKDirections alloc] initWithRequest:request];

    [directions calculateDirectionsWithCompletionHandler:
 ^(MKDirectionsResponse *response, NSError *error) {
         if (error) {
             // Handle Error
         } else {
             [self showRoute:response];
         }
     }];

    }

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

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
    MKPolylineRenderer *renderer =
    [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    renderer.strokeColor = [UIColor blueColor];
    renderer.lineWidth = 5.0;
    return renderer;
}

Now the problem is that nothing is being drawn. When the application is run my location is being shown but nothing happens afterwards. I appreciate any kind of help thank you!

Timo Cengiz
  • 3,367
  • 4
  • 23
  • 45
  • 1
    Is the map view's delegate set? –  Jan 16 '15 at 15:45
  • Mygod... I had commented it out before when testing something else. Thank you it worked out fine. but why is no PlaceMark being shown?. Also is it hard to make something like apple's iphones have like to press start button so the voice can talk where you need to go? @Anna – Timo Cengiz Jan 16 '15 at 16:44
  • Placemarks are not part of the polyline overlay object. You have to add them separately yourself using addAnnotation. It's hard to replicate the built-in directions feature but you can instead just call that feature from your app and let the actual Maps app do it for you. See http://stackoverflow.com/questions/12504294/programmatically-open-maps-app-in-ios-6. –  Jan 16 '15 at 16:54
  • Yea i dont want to do that because then the user would get away from my app which i dont want :/, I guess ill use it without the voice. Thank you for your help @Anna – Timo Cengiz Jan 16 '15 at 17:02

0 Answers0