【问题标题】:Draw polyline between given points on the map在地图上给定点之间绘制折线
【发布时间】:2012-08-16 19:31:53
【问题描述】:

我正在实现一个 iOS 应用程序,我想在地图上的几个给定坐标之间绘制一条折线。

我编写了代码并得到了从我的点开始绘制的多段线,达到了一个无限点。换句话说,线的起点从我给定的纬度和经度点开始,但线的终点是无限的,而不是另一点。

这是我的代码...

我在名为routeLatitudesNSMutableArray 中填写了坐标。数组单元格被填充一个纬度和一个经度。

MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [routeLatitudes count]); 

for(int idx = 0; idx < [routeLatitudes count]; idx=idx+2)
{
    CLLocationCoordinate2D workingCoordinate;       
    workingCoordinate.latitude=[[routeLatitudes objectAtIndex:idx] doubleValue];
    workingCoordinate.longitude=[[routeLatitudes objectAtIndex:idx+1] doubleValue];  
    MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
    pointArr[idx] = point;      
}   

// create the polyline based on the array of points. 
routeLine = [MKPolyline polylineWithPoints:pointArr count:[routeLatitudes count]];
[mapView addOverlay:self.routeLine];
free(pointArr);

和覆盖委托

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
   MKOverlayView* overlayView = nil;

  if(overlay == routeLine)
  {
    self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine]        autorelease];
    self.routeLineView.fillColor = [UIColor colorWithRed:51 green:51 blue:255  alpha:1];
    self.routeLineView.strokeColor = [UIColor colorWithRed:204 green:0 blue:0 alpha:1];
    self.routeLineView.lineWidth = 3;

    overlayView = routeLineView;
  }
return overlayView;
}

所以我需要在地图上的点之间画线。行的开头是第一个掉线的引脚,结尾是最后一个掉线的引脚。

【问题讨论】:

  • 什么是routeLineView?那是什么样的 UI 元素?

标签: objective-c ios mapkit mkoverlay


【解决方案1】:

根据代码,routeLatitudes 数组的对象如下所示:

索引 0:点 1 的纬度
索引 1:点 1 的经度
索引 2:点 2 的纬度
索引 3:点 2 的经度
索引 4:点 3 的纬度
索引 5:点 3 的经度
...

所以如果routeLatitudes.count 是 6,它实际上只有 3 个点。

这意味着malloc 分配了错误的点数,polylineWithPoints 调用也为叠加层指定了错误的点数。

另一个问题是,由于pointArr 将只包含routeLatitudes 拥有的一半对象,因此您不能对两个数组使用相同的索引值。

for 循环索引计数器 idx 在每次迭代时递增 2,因为这就是 routeLatitudes 点的布局方式,但随后使用相同的 idx 值设置 pointArr

所以对于idx=0,设置了pointArr[0],但是对于idx=2,设置了pointArr[2](而不是pointArr[1]),等等。这意味着pointArr 中的所有其他位置都未初始化,导致行“走向无穷大”。

因此修正后的代码可能如下所示:

int pointCount = [routeLatitudes count] / 2;
MKMapPoint* pointArr = malloc(sizeof(MKMapPoint) * pointCount);

int pointArrIndex = 0;  //it's simpler to keep a separate index for pointArr
for (int idx = 0; idx < [routeLatitudes count]; idx=idx+2)
{
    CLLocationCoordinate2D workingCoordinate;       
    workingCoordinate.latitude=[[routeLatitudes objectAtIndex:idx] doubleValue];
    workingCoordinate.longitude=[[routeLatitudes objectAtIndex:idx+1] doubleValue];  
    MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
    pointArr[pointArrIndex] = point;
    pointArrIndex++;
}   

// create the polyline based on the array of points. 
routeLine = [MKPolyline polylineWithPoints:pointArr count:pointCount];
[mapView addOverlay:routeLine];
free(pointArr); 

还要注意在malloc 行中,我将sizeof(CLLocationCoordinate2D) 更正为sizeof(MKMapPoint)。这在技术上并没有造成问题,因为这两个结构恰好长度相同,但使用 sizeof(MKMapPoint) 是正确的,因为这是数组将包含的内容。

【讨论】:

  • 嘿!我想问一下,如何隐藏地图上的折线?像按钮显示/隐藏折线?谢谢:)
  • 获取对覆盖层view(即MKPolylineView)的引用并将其隐藏属性设置为YES/NO。当然,您也可以只删除/添加叠加层(即 MKPolyline)。如果需要更多详细信息或它不起作用,请使用您尝试过的代码提出一个新问题。
  • 老兄,我还需要从地图上删除折线,而不仅仅是显示/隐藏。非常感谢您的帮助:)
  • 请开始一个新问题,详细说明您正在尝试做什么、您尝试了什么(以及为什么它不起作用、确切的错误消息等)。
  • @AnnaKarenina 嗨。我在两点之间画了一条线,它显示了多条线。请帮助我处于危急情况。我可以显示我的代码吗?
猜你喜欢
  • 1970-01-01
  • 2022-12-30
  • 2021-04-14
  • 1970-01-01
  • 2013-05-23
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多