在地图上的给定点之间绘制折线 [英] Draw polyline between given points on the map
问题描述
我正在实现一个iOS应用程序,我想在地图上的多个给定坐标之间绘制一条折线.
I am implementing an iOS application, and I want to draw a polyline between several given coordinates on the map.
我编写了代码,并从我的点到一个无限点绘制了折线.换句话说,线的起点是从我给定的经度和纬度点开始的,但是线的终点是无限的,而不是其他点.
I wrote the code and got the polylines being drawn from my point reaching an infinite point. In other words the beginning point of the line starts from the my given lat and long point, but the end of the line is infinite and not the other point.
这是我的代码...
我在称为routeLatitudes
的NSMutableArray
中填充了坐标.阵列单元被填充为一个纬度和一个经度.
I filled the coordinates in a NSMutableArray
called routeLatitudes
. The array cells are being filled one for latitude and one for longitude.
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;
}
因此,我需要在地图上的点之间绘制线条.该行的开头是第一个放置的引脚,末端在最后一个放置的引脚上.
So I need the lines to be drawn between the points over the map. The beginning of the line is the first dropped pin, and the end is on the last dropped pin.
推荐答案
根据代码,routeLatitudes
数组具有如下列出的对象:
According to the code, the routeLatitudes
array has objects listed like this:
索引0:点1的纬度
索引1:点1的经度
索引2:第2点的纬度
索引3:点2的经度
索引4:第3点的纬度
索引5:点3的经度
...
index 0: latitude for point 1
index 1: longitude for point 1
index 2: latitude for point 2
index 3: longitude for point 2
index 4: latitude for point 3
index 5: longitude for point 3
...
因此,如果routeLatitudes.count
为6,则实际上只有3分.
So if routeLatitudes.count
is 6, it actually has only 3 points.
这意味着malloc
分配了错误的点数,而polylineWithPoints
调用也为叠加层指定了错误的点数.
This means the malloc
is allocating the wrong number of points and the polylineWithPoints
call is also specifying the wrong number of points for the overlay.
另一个问题是,由于pointArr
将仅包含routeLatitudes
具有的对象的一半,因此不能对两个数组使用相同的索引值.
The other problem is that since pointArr
will contain only half the objects that routeLatitudes
has, you can't use the same index value for both arrays.
for
循环索引计数器idx
在每次迭代时都会增加2,因为这是routeLatitudes
点布局的方式,但是随后使用相同的idx
值来设置pointArr
.
The for
loop index counter idx
is being incremented by 2 at each iteration because that's how the routeLatitudes
points are layed out but then the same idx
value is used to set pointArr
.
因此,为idx=0
设置了pointArr[0]
,然后为idx=2
设置了pointArr[2]
(而不是pointArr[1]
),依此类推.这意味着pointArr
中的所有其他位置均未初始化,从而导致行变为无穷大".
So for idx=0
, pointArr[0]
is set but then for idx=2
, pointArr[2]
is set (instead of pointArr[1]
), and so on. This means every other position in pointArr
is left uninitialized resulting in the lines "going to infinity".
因此,更正后的代码可能如下所示:
So the corrected code might look like this:
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)
是正确的,因为这就是数组要包含的内容.
Also note in the malloc
line, I corrected sizeof(CLLocationCoordinate2D)
to sizeof(MKMapPoint)
. This technically wasn't causing a problem because those two structs happen to be the same length but it's correct to use sizeof(MKMapPoint)
since that's what the array is going to contain.
这篇关于在地图上的给定点之间绘制折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!