ios – 在MapView上渲染多条折线

我有两组坐标,并尝试在地图视图上绘制折线.我能够用一组坐标绘制折线,但我无法绘制两条折线.

以下是代码……

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 []以及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委托方法中,代码使用外部变量routePointer来设置行的颜色.不建议这样做.您不能假设调用委托方法的时间或频率.无法保证在执行addOverlay后立即调用委托方法.对于相同的叠加,也可以多次调用委托方法.

因此,必须使用与传入叠加层对象直接关联的数据来设置渲染器的属性.在当前示例中,更好的选择是将每个折线的title属性设置为“one”或“second”,并消除routePointer变量.
>在rendererForOverlay委托方法中,最好首先检查覆盖是否为MKPolyline类型,然后再将其视为一个.您可能稍后想要添加其他类型的叠加层,例如MKCircle或MKPolygon.
>目前,代码正在为每条路线中的每个线段创建单独的MKPolyline.因此,如果路由“one”有10个线段而路由“second”有15个,则代码当前添加了25个覆盖.这不是必需的. MKPolyline可以绘制多个线段.将路径的所有坐标添加到数组中并在循环之后创建并添加MKPolyline会更有效.这样,你只会添加2个叠加层.

为每条路线创建单个叠加层的示例,而不是为每条路线创建多个叠加层(路线中的每个线段都有一个叠加层):

//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);

>没有必要为纬度和经度定义单独的数组,其中每个数组都是双精度数列表.它会更有效并且会显着简化代码,以便为每个路由保留单个坐标数组(类型为CLLocationCoordinate2D).然后可以从该数组创建MKPolyline而无需任何循环.

为每个路由使用单个CLLocationCoordinate2Ds数组的示例:

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);
点赞