28

我有一个要在地图上绘制的点数组,它已经解码:

- (void) drawRoute:(NSArray *) path {
    NSInteger numberOfSteps = path.count;

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

         coordinates[index] = coordinate;
    }

    MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [map addOverlay:polyLine];
}

其中“map”是 MKMapView 的一个实例,而 path 是表示已解码的点集的数组。

我以为用这条线[map addOverlay:polyLine];会画出来。我在某些页面中看到过这种方法:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
    polylineView.strokeColor = [UIColor redColor];
    polylineView.lineWidth = 1.0;

    return polylineView;
}

polylineView 是地图上实际绘制的内容吗?我还尝试将 MKPolyline(来自上述方法)传递给最后一种方法的“<MKOverlay> 覆盖”参数,但会引发异常。

我想我已经接近了,但我不知道现在该怎么办。

请帮忙!非常感谢您提前。

4

3 回答 3

14

Done.

Was a very stupid thing, i didn't set the delegate for the MapView. Simply adding [map setDelegate:self]; did the trick.

Thank you anyway!.

于 2012-06-06T09:39:30.410 回答
4

只需使用坐标创建 MKPolyline 并将该折线添加到地图视图。

MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [map addOverlay:polyLine];

You will find an tutorial here on how to draw polyline over some coordinates. Edit: The url does not seems valid anymore. You can find the archived version of this url here.

于 2012-06-06T09:34:48.613 回答
0
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:self.allPins.count];
[self.mapView addOverlay:polyline];

Check this http://pinkstone.co.uk/how-to-draw-an-mkpolyline-on-a-map-view/ and for swift http://rshankar.com/how-to-add-mapview-annotation-and-draw-polyline-in-swift/

于 2012-06-06T09:37:49.817 回答