微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 从mapView中删除polyLine

我读了很多关于它的帖子,但我仍有问题.
这是我在两点之间绘制polyLine的代码
-(void) drawAline:(CLLocation*)newLocation
{
//drawing a line
CLLocationCoordinate2D coordinateArray[2];
coordinateArray[0] = CLLocationCoordinate2DMake(newLocation.coordinate.latitude,newLocation.coordinate.longitude);
coordinateArray[1] = CLLocationCoordinate2DMake(self.jerusalem.coordinate.latitude,self.jerusalem.coordinate.longitude);

self.routeLine = [MKpolyline polylineWithCoordinates:coordinateArray count:2];
[self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]];
[self.mapView addOverlay:self.routeLine];

}

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if(overlay == self.routeLine)
{
    if(nil == self.routeLineView)
    {
        self.routeLineView = [[MKpolylineView alloc] initWithpolyline:self.routeLine];
        self.routeLineView.fillColor = [UIColor blueColor];
        self.routeLineView.strokeColor = [UIColor blueColor];
        self.routeLineView.linewidth = 5;
    }
    return self.routeLineView;
}

return nil;

}

多数民众赞成.
问题是删除该行.
一个代码不起作用:

for (id<MKOverlay> overlayToRemove in self.mapView.overlays)
{
    if ([overlayToRemove isKindOfClass:[MKpolylineView class]])
    {
        [mapView removeOverlay:overlayToRemove];
    }
}

一个代码既不起作用:

if (self.routeLine)
{
[self.mapView removeOverlay:self.routeLine];
    self.routeLineView = nil;
    self.routeLine = nil;
}

谢谢!

解决方法

在循环遍历地图视图的叠加数组的代码中,这一行是问题所在:
if ([overlayToRemove isKindOfClass:[MKpolylineView class]])

地图视图的叠加数组包含类型为id< MKOverlay>的对象. (for循环正确地声明overlayToRemove).

因此,覆盖数组包含叠加层的模型对象,而不包含视图.

MKpolylineView类是MKpolyline叠加模型的视图.

所以if条件应该是:

if ([overlayToRemove isKindOfClass:[MKpolyline class]])

请注意,这样的循环将从地图中删除所有折线.如果要删除特定的折线,可以在添加时在每个折线上设置标题,然后在删除之前进行检查.

直接检查和删除self.routeLine的第二段代码应该有效,只要self.routeLine不是nil并且包含对当前在地图上的叠加层的有效引用.

如果地图上只有一个叠加层(一条折线),您也可以调用removeOverlays来删除地图中的所有叠加层(无论它们是什么):

[self.mapView removeOverlays:self.mapView.overlays];

原文地址:https://www.jb51.cc/iOS/330740.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐