5

我有两组坐标并试图在地图视图上绘制折线。我可以用一组坐标绘制一条折线,但我无法绘制两条折线。

下面是代码...

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

1 回答 1

4

主要问题在第二个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])

第二行不可见(或不在您期望的位置),因为它获取了错误的坐标。


但是,我想指出一些额外的事情(与主要问题无关或无关):

  1. rendererForOverlay委托方法中,代码使用外部变量routePointer来设置线条的颜色。不建议这样做。 您不能假设何时或多久调用一次委托方法。不能保证在 do 之后会立即调用委托方法addOverlay。也可以为同一个叠加层多次调用委托方法。

    因此,您必须使用与传入overlay对象直接关联的数据来设置渲染器的属性。在当前示例中,更好的选择是将每条折线的title属性设置为“一”或“二”并消除routePointer变量。

  2. rendererForOverlay委托方法中,最好先检查是否overlay是类型MKPolyline,然后再将其视为类型。您稍后可能想要添加其他类型的叠加层,例如MKCircleMKPolygon

  3. 目前,代码正在为每条路线中的每条线段创建一个MKPolyline单独。因此,如果路线“一”有 10 个线段,而路线“第二”有 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);
    
  4. 不必为纬度和经度定义单独的数组,其中每个数组都是双精度列表。它会更有效率,并将显着简化代码以保留单个坐标数组(类型CLLocationCoordinate2D为每条路线保留一个坐标数组(类型为 )。MKPolyline然后可以从该数组创建一个,而无需任何循环。

    CLLocationCoordinate2D为每条路线使用单个 s 数组的示例:

    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);
    
于 2014-11-25T15:52:38.283 回答