【发布时间】:2012-08-16 19:31:53
【问题描述】:
我正在实现一个 iOS 应用程序,我想在地图上的几个给定坐标之间绘制一条折线。
我编写了代码并得到了从我的点开始绘制的多段线,达到了一个无限点。换句话说,线的起点从我给定的纬度和经度点开始,但线的终点是无限的,而不是另一点。
这是我的代码...
我在名为routeLatitudes 的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;
}
所以我需要在地图上的点之间画线。行的开头是第一个掉线的引脚,结尾是最后一个掉线的引脚。
【问题讨论】:
-
什么是routeLineView?那是什么样的 UI 元素?
标签: objective-c ios mapkit mkoverlay