如何在MapView上绘制MKPolyline? [英] How to draw a MKPolyline on a MapView?
问题描述
我有一个要在地图上绘制的点数组,它已经被解码了:
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];
}
其中map是MKMapView的一个实例,而数组的路径代表已解码的点集。
where "map" is an instance of MKMapView, and path the array representing the already decoded set of points.
我认为用 [map addOverlay:polyLine];
这一行就可以了。我在某些页面中看到过这种方法:
I thought that with the line [map addOverlay:polyLine];
it would be drawn. I've seen in some pages this method:
- (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> overlay参数,但抛出异常。
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.
我想我很接近,但我现在不知道该怎么办。
I think I'm close, but I don't know what to do now.
请帮忙!非常感谢你提前。
Please help! Thank you very much in advance.
推荐答案
完成。
这是一个非常愚蠢的事情,我没有为MapView设置委托。只需添加[map setDelegate:self];诀窍。
Was a very stupid thing, i didn't set the delegate for the MapView. Simply adding [map setDelegate:self]; did the trick.
无论如何,谢谢!。
这篇关于如何在MapView上绘制MKPolyline?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!