这让我发疯了。我已经浏览了 stackoveflow 上的所有帖子,但没有任何内容符合要求。我正在尝试添加一个简单的折线(即不是自定义叠加)作为我的MKMapView
. viewForOverlay
委托上的方法永远不会被调用。地图委托被正确地为所有其他委托函数调用。以下是该viewForOverlay
方法的代码:
//maanges the overlay
- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay{
NSLog(@"does it ask for the overlay view?");
MKOverlayView *overlayView = nil;
return overlayView;
}
这是我构造折线并将其添加到地图的代码:
MKPolyline *thePolyline = [MKPolyline polylineWithPoints:pts count:[arrOfPoints count]];
[thePolyline setTitle:@"line"];
[mapView addOverlay:thePolyline];
折线实际上确实有我的点集合(大约 1000 个),所以我认为问题不存在。我是否缺少地图视图上的一些必需属性或其他一些实现?
编辑显示折线MKMapPoint
生成的代码:
我使用一个包含大约 1100 个点的 xml 文件来生成折线,作为 appConfig 过程的一部分。NSXMLParser
我分别用和读取和解析文件NSXMLParserDelegate
。这是生成点的代码(来自协议foundCharacters
中的方法NSXMLParserDelegate
):
//NSXMLParserDelegate methods...
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if(POINT){
NSArray *arr = [string componentsSeparatedByString:@","];
MKMapPoint pt = MKMapPointMake([[arr objectAtIndex:1]doubleValue], [[arr objectAtIndex:0]doubleValue]);
MapPointObject *thePoint = [[MapPointObject alloc] init];
thePoint.mapPoint = pt;
//gives the mkmappoint to the array of points.
[arrOfPoints addObject:thePoint];
[thePoint release];
}
}
这是点实际生成MKPolyline
并将其提供给 mapView 的位置(来自协议didEndElement
上的方法NSXMLParserDelegate
):
if([elementName isEqualToString:@"appConfig"]){
MKMapPoint *pts = malloc([arrOfPoints count] * sizeof(MKMapPoint));
for(int i = 0; i <= [arrOfPoints count] - 1; i++){
MapPointObject *pointObject = [arrOfPoints objectAtIndex:i];
pts[i] = pointObject.mapPoint;
}
MKPolyline *thePolyline = [MKPolyline polylineWithPoints:pts count:[arrOfPoints count]];
[thePolyline setTitle:@"line"];
//adding the polyline to the model's mapview
Model *theModel = [Model sharedModel];
[theModel.mapView setVisibleMapRect:thePolyline.boundingMapRect animated:YES];
[theModel.mapView addOverlay:thePolyline];
free(pts);
}
上的点数属性MKPolyline
实际上表示其中有 1100 个点。
编辑:示例 XML 值:
<appConfig>
<point>-94.847587,38.977967</point>
<point>-94.844111,38.977978</point>
<point>-94.844108,38.977369</point>
<point>-94.844003,38.977369</point>
<point>-94.843955,38.974886</point>