How to draw a route line on a map?

I mention the code below.

 MKPlacemark *source = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(37.33554,-121.885209) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];

MKMapItem *srcMapItem = [[MKMapItem alloc]initWithPlacemark:source];
[srcMapItem setName:@""];

MKPlacemark *destination = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(latttt, lonnn) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];

MKMapItem *distMapItem = [[MKMapItem alloc]initWithPlacemark:destination];
[distMapItem setName:@""];


MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
[request setSource:srcMapItem];
[request setDestination:distMapItem];
[request setTransportType:MKDirectionsTransportTypeAutomobile];

MKDirections *direction = [[MKDirections alloc]initWithRequest:request];

[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

    NSLog(@"response = %@",response);
    NSArray *arrRoutes = [response routes];
    [arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        MKRoute *rout = obj;

        MKPolyline *line = [rout polyline];



        [self.mapView setVisibleMapRect:[line boundingMapRect]];
        [self.mapView addOverlay:line];
        NSLog(@"Rout Name : %@",rout.name);
        NSLog(@"Total Distance (in Meters) :%f",rout.distance);

        NSArray *steps = [rout steps];

        NSLog(@"Total Steps : %lu",(unsigned long)[steps count]);

        [steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            [arr_direction addObject:[obj instructions]];
           // NSLog(@"%@",arr_direction);


            int latt=[obj distance];

            NSString *list_la=[NSString stringWithFormat:@"%d",latt];


                 [arr_distance addObject:list_la];


            // NSLog(@"%@",arr_distance);
        }];
        [directions_table reloadData ];
    }];
}];

and viewForOverlay function

    - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay {

if ([overlay isKindOfClass:[MKPolyline class]]) {
    MKPolylineView* aView = [[MKPolylineView alloc]initWithPolyline:(MKPolyline*)overlay] ;
    aView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:1.0];
    aView.lineWidth = 7;
    return aView;
}
return nil;

}

Screenshot:

my output screen using this code

specific place

and my question is to draw a line between the user's location and the road, road and destination. Check out this image.

thanks for the support...

+4
source share
1 answer

Make a line between the user's location and start point, end point and destination.

[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

    NSLog(@"response = %@",response);
    NSArray *arrRoutes = [response routes];
    [arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        MKRoute *route = obj;

        MKPolyline *line = [route polyline];

        [self.mapView setVisibleMapRect:[line boundingMapRect]];
        [self.mapView addOverlay:line];
        NSLog(@"Rout Name : %@",route.name);
        NSLog(@"Total Distance (in Meters) :%f",route.distance);


        ////// EDITED FROM HERE by Kosuke //////
        NSUInteger pointCount = route.polyline.pointCount;

        // allocate a C array to hold this many points/coordinates...
        CLLocationCoordinate2D *routeCoordinates
        = malloc(pointCount * sizeof(CLLocationCoordinate2D));

        // get the coordinates (all of them)...
        [route.polyline getCoordinates:routeCoordinates
                                 range:NSMakeRange(0, pointCount)];

        // make line between User location and Start point
        CLLocationCoordinate2D coordinates1[2] = {userCoordinate, routeCoordinates[0]};
        MKPolyline *line1 = [MKPolyline polylineWithCoordinates:coordinates1 count:2];
        [self.mapView addOverlay:line1];

        // make line between Finish point and Destination location
        CLLocationCoordinate2D coordinates2[2] = {routeCoordinates[pointCount - 1], destinationCoordinate};
        MKPolyline *line2 = [MKPolyline polylineWithCoordinates:coordinates2 count:2];
        [self.mapView addOverlay:line2];

        //free the memory used by the C array when done with it...
        free(routeCoordinates);

        //////////////////

        NSArray *steps = [rout steps];

        NSLog(@"Total Steps : %lu",(unsigned long)[steps count]);

        [steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

enter image description here

+3
source

Source: https://habr.com/ru/post/1612536/


All Articles