【发布时间】:2018-10-18 22:04:20
【问题描述】:
我有以下代码,我试图用它在一组坐标之间绘制一条折线(这是正确的,因为我也使用它们将图钉添加到地图上,并且工作正常)。
我这样调用一个绘图方法来启动绘图(方法调用中的数组包含必要的坐标):
[self drawRoute:[[transportData objectForKey:@"19"] objectForKey:@"stops"]];
这是应该在地图上绘制线的实际方法(selectedRoute 是一个 MKPolyline 对象):
- (void)drawRoute:(NSArray *)routePointsArray {
if (selectedRoute) {
[mapView removeOverlay:selectedRoute];
selectedRoute = nil;
}
CLLocationCoordinate2D routeCoordinates[routePointsArray.count];
for (int i = 0; i < routePointsArray.count; i++) {
float latitude = [[[routePointsArray objectAtIndex:i] objectForKey:@"lat"] floatValue];
float longitude = [[[routePointsArray objectAtIndex:i] objectForKey:@"lon"] floatValue];
CLLocationCoordinate2D routePoint = CLLocationCoordinate2DMake(latitude, longitude);
routeCoordinates[i] = routePoint;
}
selectedRoute = [MKPolyline polylineWithCoordinates:routeCoordinates count:routePointsArray.count];
[mapView addOverlay:selectedRoute];
[mapView setVisibleMapRect:[selectedRoute boundingMapRect]];
}
这是我的代表:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKPolylineRenderer *routeLineView = [[MKPolylineRenderer alloc] initWithPolyline:selectedRoute];
if(overlay == selectedRoute)
{
if(nil == routeLineView)
{
routeLineView = [[MKPolylineRenderer alloc] initWithPolyline:selectedRoute];
routeLineView.fillColor = [UIColor redColor];
routeLineView.strokeColor = [UIColor redColor];
routeLineView.lineWidth = 5;
}
return routeLineView;
}
return nil;
}
我把它缩小到 routeCoordinates 数组没有被坐标填满,但我不明白为什么。
另外,如果您在代码中发现任何错误,如果您能指出这些错误(可能提供解决方案),我将不胜感激,因为我正在学习 iOS 的这一部分并且可以使用我能获得的任何帮助。
【问题讨论】:
-
您创建一个空的
routeLineView,然后在添加覆盖线之前将其与nil(不会是这样)进行比较。 -
是的,我不太明白那部分。但你是对的,如果我去掉那个条件,这条线就会被画出来。
标签: ios objective-c mkmapview mkpolyline