• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - viewforoverlay 永远不会被调用

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:13:32 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这让我发疯了。我已经浏览了 stackoveflow 上的所有帖子,但没有任何内容符合要求。我正在尝试添加一个简单的折线(即不是自定义叠加)作为我的 MKMapView 的叠加。委托(delegate)上的 viewForOverlay 方法永远不会被调用。 map 委托(delegate)被正确地为所有其他委托(delegate)函数调用。以下是 viewForOverlay 方法的代码:

//maanges the overlay
- (MKOverlayView *)mapViewMKMapView *)map viewForOverlayid <MKOverlay>)overlay{

    NSLog(@"does it ask for the overlay view?");

    MKOverlayView *overlayView = nil;

    return overlayView;
}

这是我构造折线并将其添加到 map 的代码:

    MKPolyline *thePolyline = [MKPolyline polylineWithPoints:pts count:[arrOfPoints count]];

    [thePolyline setTitle"line"];

    [mapView addOverlay:thePolyline];

实际上,折线确实有我的点集合(大约 1000 个),所以我认为问题不存在。我是否缺少 map View 上的某些必需属性或其他一些实现?

EDIT 显示折线 MKMapPoint 生成的代码:

我使用一个包含大约 1100 个点的 xml 文件来生成折线,作为 appConfig 过程的一部分。我分别使用 NSXMLParserNSXMLParserDelegate 读取和解析文件。下面是生成点的代码(来自 NSXMLParserDelegate 协议(protocol)中的 foundCharacters 方法):

//NSXMLParserDelegate methods...
- (void)parserNSXMLParser *)parser foundCharactersNSString *)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 的位置 (来自 NSXMLParserDelegate 协议(protocol)上的 didEndElement 方法):

   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>



Best Answer-推荐答案


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

你应该存储坐标而不是 MKMapPoint 值(你是)。

所以不要使用 MKMapPointpolylineWithPoints,而是使用 CLLocationCoordinate2DpolylineWithCoordinates

在xml解析器方法中,使用CLLocationCoordinate2DMake创建并存储一个CLLocationCoordinate2D

pts 数组的类型应该是 CLLocationCoordinate2D * 并且在执行 malloc 时,使用 sizeof(CLLocationCoordinate2D).

然后调用 polylineWithCoordinates 而不是 polylineWithPoints


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

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

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap