ios 如何在 MapView 上绘制 MKPolyline?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10911534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw a MKPolyline on a MapView?
提问by Fustigador
I have an array of points to be drawn on a map, its already decoded:
我有一个要在地图上绘制的点数组,它已经解码了:
- (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];
}
where "map" is an instance of MKMapView, and path the array representing the already decoded set of points.
其中“map”是 MKMapView 的一个实例,并路径表示已解码的一组点的数组。
I thought that with the line [map addOverlay:polyLine];
it would be drawn. I've seen in some pages this method:
我以为用这条线[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;
}
Is the polylineView what is actually drawn on map? I've tried also to pass the MKPolyline (from the method above) to the "<MKOverlay> overlay" argument of this last method, but throws an exception.
polylineView 是地图上实际绘制的内容吗?我还尝试将 MKPolyline(来自上面的方法)传递给最后一个方法的“<MKOverlay> overlay”参数,但抛出异常。
I think I'm close, but I don't know what to do now.
我想我已经接近了,但我现在不知道该怎么办。
Please help! Thank you very much in advance.
请帮忙!非常感谢您提前。
采纳答案by Fustigador
Done.
完毕。
Was a very stupid thing, i didn't set the delegate for the MapView. Simply adding [map setDelegate:self]; did the trick.
是一件非常愚蠢的事情,我没有为 MapView 设置委托。只需添加 [map setDelegate:self]; 成功了。
Thank you anyway!.
还是很感谢你!。
回答by Abdullah Md. Zubair
Just create MKPolyline with coordinates & add that polyLine to map view.
只需使用坐标创建 MKPolyline 并将该 polyLine 添加到地图视图。
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[map addOverlay:polyLine];
You will find an tutorial hereon 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.
回答by Arvind
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/
检查这个http://pinkstone.co.uk/how-to-draw-an-mkpolyline-on-a-map-view/和快速http://rshankar.com/how-to-add-mapview-annotation -and-draw-polyline-in-swift/