简体   繁体   English

如何在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];
}

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]; 我认为用行[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;
}

Is the polylineView what is actually drawn on map? polylineView在地图上实际绘制的是什么? 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. 我也尝试将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. 非常感谢你提前。

Done. 完成。

Was a very stupid thing, i didn't set the delegate for the MapView. 是一个非常愚蠢的事情,我没有为MapView设置委托。 Simply adding [map setDelegate:self]; 只需添加[map setDelegate:self]; did the trick. 做了伎俩。

Thank you anyway!. 还是要谢谢你!。

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 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 . 您可以在此处找到此网址的存档版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM