我有两组坐标,并尝试在 map View 上绘制折线。我可以用一组坐标绘制一条折线,但是我不能绘制两条折线。
下面是代码...
func drawRouteOnMap()
{
var centerCoordinate : CLLocationCoordinate2D = CLLocationCoordinate2DMake(icRouteLat[0], icRouteLong[0])
let span = MKCoordinateSpanMake(0.001, 0.001)
var centerPosition = MKCoordinateRegionMake(centerCoordinate, span)
mapView.setRegion(centerPosition,animated:true)
self.mapView.mapType = MKMapType.Hybrid
routePointer = "one";
// first route line
//----------able to draw polyline with this set of coordinates
for i in 0..<routeLat.count-1
{
var fromCoordinate :CLLocation = CLLocation(latitude: routeLat[i], longitude: routeLong[i])
var toCoordinate :CLLocation = CLLocation(latitude: routeLat[i+1], longitude: routeLong[i+1])
var locations = [fromCoordinate, toCoordinate];
var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate});
var polyLine = MKPolyline(coordinates: &coordinates, count: locations.count);
mapView.addOverlay(polyLine);
}
routePointer = "second";
// IC route line
//------Polyline for this set of coordinates doesn't appear on map view
for j in 0..<icRouteLat.count-1
{
var fromCoordinateIC :CLLocation = CLLocation(latitude: icRouteLat[j], longitude: icRouteLat[j])
var toCoordinateIC :CLLocation = CLLocation(latitude: icRouteLong[j+1], longitude: icRouteLong[j+1])
var locationsIC = [fromCoordinateIC, toCoordinateIC];
var coordinatesIC = locationsIC.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate});
var polyLineIC = MKPolyline(coordinates: &coordinatesIC, count: locationsIC.count);
mapView.addOverlay(polyLineIC);
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
let route: MKPolyline = overlay as MKPolyline
let routeRenderer = MKPolylineRenderer(polyline:route)
routeRenderer.lineWidth = 3.0
if routePointer == "one"
{
routeRenderer.strokeColor = UIColor(red: 240.0/255.0, green: 68.0/255.0, blue: 0.0/255.0, alpha: 1);
}
else if routePointer == "second"
{
routeRenderer.strokeColor = UIColor(red: 45.0/255.0, green: 200.0/255.0, blue: 0.0/255.0, alpha: 1);
}
return routeRenderer
}
数组
routeLat[], routeLong[]
和icRouteLat[] and icRouteLong[]
是两组坐标。有什么我想念的吗?还是这是正确的实现方式?
编辑
根据建议更新了代码...
func drawRouteOnMap()
{
var centerCoordinate : CLLocationCoordinate2D = CLLocationCoordinate2DMake(icRouteLat[0], icRouteLong[0])
let span = MKCoordinateSpanMake(0.001, 0.001)
var centerPosition = MKCoordinateRegionMake(centerCoordinate, span)
mapView.setRegion(centerPosition,animated:true)
self.mapView.mapType = MKMapType.Hybrid
var polyLine: MKPolyline!
var polyLineIC: MKPolyline!
// first route line
for i in 0..<routeLat.count-1
{
var fromCoordinate :CLLocation = CLLocation(latitude: routeLat[i], longitude: routeLong[i])
var toCoordinate :CLLocation = CLLocation(latitude: routeLat[i+1], longitude: routeLong[i+1])
var locations = [fromCoordinate, toCoordinate];
var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate});
polyLine = MKPolyline(coordinates: &coordinates, count: locations.count);
polyLine.title = "one";
mapView.addOverlay(polyLine);
}
// IC route line
for j in 0..<icRouteLat.count-1
{
var fromCoordinateIC :CLLocation = CLLocation(latitude: icRouteLat[j], longitude: icRouteLong[j])
var toCoordinateIC :CLLocation = CLLocation(latitude: icRouteLat[j+1], longitude: icRouteLong[j+1])
var locationsIC = [fromCoordinateIC, toCoordinateIC];
var coordinatesIC = locationsIC.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate});
polyLineIC = MKPolyline(coordinates: &coordinatesIC, count: locationsIC.count);
polyLineIC.title = "ic";
mapView.addOverlay(polyLineIC);
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline
{
let route: MKPolyline = overlay as MKPolyline
let routeRenderer = MKPolylineRenderer(polyline:route)
routeRenderer.lineWidth = 3.0
if overlay.title == "one"
{
routeRenderer.strokeColor = UIColor(red: 240.0/255.0, green: 68.0/255.0, blue: 0.0/255.0, alpha: 1);
}
else if overlay.title == "ic"
{
routeRenderer.strokeColor = UIColor(red: 45.0/255.0, green: 200.0/255.0, blue: 0.0/255.0, alpha: 1);
}
return routeRenderer
}
return nil
}
最终编辑
更新的代码:
func drawRouteOnMap()
{
var centerCoordinate : CLLocationCoordinate2D = icRoute[0];
let span = MKCoordinateSpanMake(0.001, 0.001)
var centerPosition = MKCoordinateRegionMake(centerCoordinate, span)
mapView.setRegion(centerPosition,animated:true)
self.mapView.mapType = MKMapType.Hybrid
// first route line
polyLine = MKPolyline(coordinates: &firstRoute, count: firstRoute.count);
polyLine.title = "one";
mapView.addOverlay(polyLine);
// IC route line
polyLine = MKPolyline(coordinates: &icRoute, count: icRoute.count);
polyLine.title = "ic";
mapView.addOverlay(polyLine);
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline
{
let route: MKPolyline = overlay as MKPolyline
let routeRenderer = MKPolylineRenderer(polyline:route)
routeRenderer.lineWidth = 3.0
if overlay.title == "one"
{
routeRenderer.strokeColor = UIColor(red: 240.0/255.0, green: 68.0/255.0, blue: 0.0/255.0, alpha: 1);
}
else if overlay.title == "ic"
{
routeRenderer.strokeColor = UIColor(red: 45.0/255.0, green: 200.0/255.0, blue: 0.0/255.0, alpha: 1);
}
return routeRenderer
}
return nil
}
最佳答案
主要问题在第二个for
循环中:
var fromCoordinateIC :CLLocation =
CLLocation(latitude: icRouteLat[j],
longitude: icRouteLat[j]) //<-- should be icRouteLong
var toCoordinateIC :CLLocation =
CLLocation(latitude: icRouteLong[j+1], //<-- should be icRouteLat
longitude: icRouteLong[j+1])
第二行不可见(或者不在您期望的位置),因为它得到了错误的坐标。
但是,我想指出一些其他问题(与主要问题无关或与之无关):
rendererForOverlay
委托(delegate)方法中,代码使用外部变量routePointer
设置行的颜色。不建议这样做。您不能假定何时或多长时间调用一次委托(delegate)方法。不能保证在执行addOverlay
之后将立即调用委托(delegate)方法。对于相同的叠加层,也有可能多次调用委托(delegate)方法。因此,必须使用与传入的
overlay
对象直接关联的数据来设置渲染器的属性。在当前示例中,更好的选择是将每条折线的title
属性设置为“一个”或“第二”,并消除routePointer
变量。 rendererForOverlay
委托(delegate)方法中,最好先检查overlay
是否为MKPolyline
类型,然后再将其视为MKCircle
类型。您稍后可能要添加其他类型的叠加层,例如MKPolygon
或MKPolyline
。 MKPolyline
。因此,如果路线“一个”具有10个线段,而路线“第二个”具有15个线段,则该代码当前添加了25个叠加层。这不是必需的。 MKPolyline
可以绘制多个线段。将路线的所有坐标添加到数组中,然后在循环之后创建并添加CLLocationCoordinate2D
会更加有效。这样,您将只添加2个叠加层。为每个路线创建一个覆盖而不是为每个路线创建多个覆盖(该 route 的每个线段一个)的示例:
//First add all the coordinates to an array...
var coordinates: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
for i in 0 ..< routeLat.count
{
var coordinate = CLLocationCoordinate2DMake(routeLat[i], routeLong[i])
coordinates.append(coordinate)
}
//Then create a single overlay with ALL the coordinates in the route...
polyLine = MKPolyline(coordinates: &coordinates, count: coordinates.count);
polyLine.title = "one";
mapView.addOverlay(polyLine);
MKPolyline
类型)。然后可以从该数组创建一个CLLocationCoordinate2D
而不进行任何循环。对每个路由使用ojit_code的单个数组的示例:
var routeOneCoordinates = [CLLocationCoordinate2DMake(30.0, -87.0),
CLLocationCoordinate2DMake(32.0, -84.0),
CLLocationCoordinate2DMake(31.5, -83.5),
CLLocationCoordinate2DMake(31.0, -83.0),
CLLocationCoordinate2DMake(33.5, -82.0)]
polyLine = MKPolyline(coordinates: &routeOneCoordinates, count: routeOneCoordinates.count);
polyLine.title = "one";
mapView.addOverlay(polyLine);