Objective c 在地图上的给定点之间绘制多段线

Objective c 在地图上的给定点之间绘制多段线,objective-c,ios,mapkit,mkoverlay,Objective C,Ios,Mapkit,Mkoverlay,我正在实现一个iOS应用程序,我想在地图上的几个给定坐标之间绘制一条多段线 我编写了代码,得到了从我的点到无限点的多段线。换句话说,直线的起点从给定的纬度和长点开始,但直线的终点是无限的,而不是另一点 这是我的密码 我将坐标填入一个名为RouteRelationales的NSMutableArray中。正在填充阵列单元格,一个用于纬度,一个用于经度 MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [routeLatit

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

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

这是我的密码

我将坐标填入一个名为RouteRelationales的NSMutableArray中。正在填充阵列单元格,一个用于纬度,一个用于经度

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;
}

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

根据代码,RouteRelationalites数组中列出的对象如下所示:

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); 
索引0:点1的纬度 索引1:点1的经度 索引2:点2的纬度 索引3:点2的经度 索引4:第3点的纬度 索引5:点3的经度

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

这意味着malloc分配了错误的点数,而polylineWithPoints调用也为覆盖指定了错误的点数

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

for循环索引计数器idx在每次迭代时递增2,因为这是RouteRelatiences点的布局方式,但随后相同的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行中,我将SizeOfcllocationCoordinated2D更正为sizeofMKMapPoint。这在技术上并没有造成问题,因为这两个结构恰好长度相同,但使用sizeofMKMapPoint是正确的,因为这就是数组将包含的内容。

根据代码,RouteRelatiences数组中列出的对象如下所示:

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); 
索引0:点1的纬度 索引1:点1的经度 索引2:点2的纬度 索引3:点2的经度 索引4:第3点的纬度 索引5:点3的经度

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

这意味着malloc分配了错误的点数,而polylineWithPoints调用也为覆盖指定了错误的点数

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

for循环索引计数器idx在每次迭代时递增2,因为这是RouteRelatiences点的布局方式,但随后相同的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行中,我将SizeOfcllocationCoordinated2D更正为sizeofMKMapPoint。这在技术上并没有造成问题,因为这两个结构恰好长度相同,但使用sizeofMKMapPoint是正确的,因为数组将包含sizeofMKMapPoint。

嘿!我想问一下,如何在地图上隐藏多段线?像在按钮中显示/隐藏多段线?谢谢:获取覆盖视图(即MKPolylineView)的引用,并将其隐藏属性设置为“是/否”。当然,您也可以删除/添加覆盖视图(即MKPolylineView)。如果需要更多的细节或者它不起作用,请用你尝试过的代码问一个新问题。伙计,我还需要删除地图上方的多段线,而不仅仅是显示/隐藏。我真的很感谢你的帮助:请开始一个新的问题,详细说明你正在尝试做什么,你尝试了什么,为什么它不起作用,确切的错误消息,等等。@annakareninahi。我在两点之间画了一条线,它显示了多条线。请帮助我,我的情况很危急。我可以显示我的代码吗?嘿!我想问一下,如何在地图上隐藏多段线?像在按钮中显示/隐藏多段线?谢谢:获取覆盖视图(即MKPolylineView)的引用,并将其隐藏属性设置为“是/否”。当然,您也可以删除/添加覆盖视图(即MKPolylineView)。如果需要更多的细节或者它不起作用,请用你尝试过的代码问一个新问题。伙计,我还需要删除地图上方的多段线,而不仅仅是显示/隐藏。我真的很感谢你的帮助:请开始一个新的问题
n提供一些关于您正试图做什么、您尝试了什么以及为什么它不起作用、确切错误消息等的详细信息。@AnnaKarenina hi。我在两点之间画了一条线,它显示了多条线。请帮助我,我的情况很危急。我可以显示我的代码吗?路线视图是什么?这是什么类型的UI元素?routeLineView是什么?这是什么类型的UI元素?