1

I plot the route between source and destination using the following piece of code. After plotting the route sucessfully, I get a list of coordinates where I wanted to plot another route on top of existing one with different color.

The use case I am trying to code is, we set source and destination, plot the dotted route once, then plot the actual line based on the list of locations

From the code, I can see the dotted line but can not see the another line overlapping on it.

My code is:

     -(void) getDirection {

    CLLocationCoordinate2D sourceCoords = CLLocationCoordinate2DMake(42.130655, -71.041158);
    MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:sourceCoords addressDictionary:nil];
    MKMapItem *source = [[MKMapItem alloc] initWithPlacemark:sourcePlacemark];

    // Make the destination location
    CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(42.128121, -70.936151);    MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];
    MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];


    MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];
    [directionsRequest setSource:source];
    [directionsRequest setDestination:destination];

    [self sourceMarker:sourcePlacemark];
    [self destinationMarker:destinationPlacemark];
    //
    //routeCoords
    NSInteger nuberOfStesps = routeCoords.count;
    CLLocationCoordinate2D *pointsCoordinate = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * nuberOfStesps);
    for (NSInteger index=0; index < nuberOfStesps;  index++) {

        CLLocation *location = [routeCoords objectAtIndex:index];
        coordinate2 = location.coordinate;

        //NSLog(@"Location Latitide : %f", coordinate2.latitude);
        //NSLog(@"Location Longitude : %f", coordinate2.longitude);
        pointsCoordinate[index] = CLLocationCoordinate2DMake(coordinate2.latitude, coordinate2.longitude);

    }
    routeLine = [MKPolyline polylineWithCoordinates:pointsCoordinate count:nuberOfStesps];
   // free(pointsCoordinate);

    routeLine.title = @"New points";
    [self.mapView addOverlay:routeLine];
    //
    MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        if (error) {
            NSLog(@"There was an error getting your directions");
            return;

        }else {
            [self showRoute:response];

        }
    }];
}

     - (void) showRoute:(MKDirectionsResponse * ) response {

    _currentRoute = [response.routes firstObject];
    [self.mapView setVisibleMapRect:_currentRoute.polyline.boundingMapRect animated:NO];
    [self.mapView removeOverlays:self.mapView.overlays];
    [self.mapView addOverlay:_currentRoute.polyline level:MKOverlayLevelAboveRoads];


    }

    - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        [renderer setStrokeColor:[UIColor redColor]];
        [renderer setLineWidth:4.0];
        [renderer setLineDashPattern:@[@2, @5]];
        [renderer setStrokeColor:[UIColor redColor]];
        return renderer;
    }
    return nil;
    }

I followed this thread iOS Maps draw route (line) between several points (geopoints)

But it did not work for me. What am I doing wrong?

Community
  • 1
  • 1
Santosh
  • 202
  • 3
  • 16

0 Answers0