CLLocationCoordinate2D

CLLocationCoordinate2D

这让我发疯。我已经看过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流程的一部分。我分别使用NSXMLParserNSXMLParserDelegate读取和解析文件。这是生成点的代码(来自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>

最佳答案

xml文件包含坐标(纬度和经度)值。这些坐标值与MKMapPoint值不同(后者是地图视图的经度/纬度在平面地图上的x,y投影)。

您应该存储的是坐标,而不是MKMapPoint的值。

因此,不要使用MKMapPointpolylineWithPoints,而要使用CLLocationCoordinate2DpolylineWithCoordinates

在xml解析器方法中,使用CLLocationCoordinate2D创建并存储CLLocationCoordinate2DMake
pts数组的类型应为CLLocationCoordinate2D *,并且在执行malloc时,请使用sizeof(CLLocationCoordinate2D)

然后调用polylineWithCoordinates而不是polylineWithPoints

顺便说一句,在进行上述更改之后,您还需要在viewForOverlay中实际返回一个非nil的覆盖视图,否则您仍然看不到该行:

MKPolylineView *polylineView = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
polylineView.strokeColor = [UIColor redColor];
polylineView.lineWidth = 2.0;
return polylineView;

关于ios - viewforoverlay永远不会被调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10823257/

10-10 04:53