iOS:如何在现有路线上绘制折线?

时间:2015-07-03 06:56:31

标签: ios location mkmapview maproute

我使用以下代码绘制源和目标之间的路线。在成功绘制路线之后,我得到一个坐标列表,我想在另一条路线上绘制不同颜色的现有路线。

我试图编码的用例是,我们设置源和目的地,绘制一次虚线路线,然后根据位置列表绘制实际线

从代码中,我可以看到虚线,但看不到另一条重叠的线。

我的代码是:

     -(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;
    }

我遵循了这个帖子iOS Maps draw route (line) between several points (geopoints)

但它对我不起作用。我做错了什么?

0 个答案:

没有答案