Draw poly line on the road in mkmap view - iphone

2020-07-26 17:54发布

问题:

i am making an navigation based application. In this application i am drawing a route from points selected by the user.

for Calculating the route i have used Google direction API. and for drawing the route i have used this code

- (void) drawRoute:(NSArray *) path
{
    NSInteger numberOfSteps = path.count;
    [self.objMapView removeOverlays: self.objMapView.overlays];

    CLLocationCoordinate2D coordinates[numberOfSteps];
    for (NSInteger index = 0; index < numberOfSteps; index++)
    {
        CLLocation *location = [path objectAtIndex:index];
        CLLocationCoordinate2D coordinate = location.coordinate;

        coordinates[index] = coordinate;
    }

    for( id <MKOverlay> ovr in [self.objMapView overlays])
    {
        MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:ovr];


        if (polylineView.tag == 22)
        {
            [self.objMapView removeOverlay:ovr];
        }
        [polylineView release];
    }

    MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [self.objMapView addOverlay:polyLine];


}

PROBLEM :~ When i am drawing a route i encountered a problem that it is not drawing polyline on the road. To explain this i have attached an image

as shown in the picture polyline is not on the road.

I have used UICGDirections but it is not working properly some times.

Please help me i am new to mapview.

Thanks in advance

回答1:

Below is a sample IPhone application demonstrates drawing routes on MKMapView using http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@ as URL

MapWithRoutes

You can take this as a reference as it has implemented the same thing what you are trying to achieve.