2012-08-16 145 views
2

我正在实现一个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; 
} 

所以我需要在地图上的点之间绘制的线条。该行的开头是第一个丢弃的引脚,最后是最后一个丢弃的引脚。

+0

什么是routeLineView?这是什么样的UI元素? – 2013-02-04 02:11:32

回答

4

根据该代码,所述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=0pointArr[0]设置,但然后idx=2pointArr[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)是正确的,因为这是数组将包含的内容。

+0

它的工作原理非常完美。 非常感谢 – fadd 2012-08-17 11:22:46

+0

嘿!我想问,如何在地图上隐藏多段线?就像在一个按钮中显示/隐藏多段线? 谢谢:) – fadd 2012-09-03 06:50:47

+0

获取覆盖_view_(即MKPolylineView)的引用并将其隐藏属性设置为YES/NO。当然,你也可以删除/添加叠加层(即MKPolyline)。如果需要更多详细信息或无效,请使用您尝试的代码提出新问题。 – Anna 2012-09-03 13:51:10