Swift MapKit多条带不同颜色的折线。

huangapple go评论67阅读模式
英文:

Swift MapKit Multiple Polylines with different colors

问题

我正在尝试在Swift的MapKit中为每个Polyline使用不同的颜色。
以下是一个简化的示例。

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if let overlay_ = overlay as? MKPolyline {
        let renderer = MKPolylineRenderer(overlay: overlay)
        if overlay_.title == "2" {
            renderer.strokeColor = UIColor.red
        } else {
            renderer.strokeColor = UIColor.blue
        }
        renderer.lineWidth = 5
        return renderer
    } else {
        return MKOverlayRenderer()
    }
}

我在地图上有多个折线,我设置不同的overlay.title来区分颜色。
不幸的是,它总是采用最后设置的颜色并将其应用于所有折线。
在我的示例中,我有3个带有标题"0"、"1"和"2"的折线。结果是所有折线都是红色的描边颜色。
是否有人有解决此问题的想法?您需要更多的代码吗?

用于添加标题的代码如下:

func showAllRoutes(map: MKMapView, route: Routes) {
    var coordinates: [CLLocationCoordinate2D] = []

    if dm.show_all_routes {
        for x in route.routes.indices {
            for y in route.routes[x].route_numbers {
                coordinates.append(CLLocationCoordinate2D(latitude: dm.alle_staende.locations[y].latitude, longitude: dm.alle_staende.locations[y].longitude))
            }

            let overlay = MKPolyline(coordinates: coordinates, count: coordinates.count)
            overlay.subtitle = "Route"
            overlay.title = "\(x)"
            map.addOverlay(overlay)
        }
    }
}
英文:

I am trying to have different colors for each Polyline in MapKit in Swift.
Here is a simplified example.

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
		if let overlay_ = overlay as? MKPolyline {
			let renderer = MKPolylineRenderer(overlay: overlay)
            if overlay_.title == "2" {
                renderer.strokeColor = UIColor.red
            } else {
                renderer.strokeColor = UIColor.blue
            }
			renderer.lineWidth = 5
			return renderer
		} else {
			return MKOverlayRenderer()
		}
	}

I have multiple polylines in a map where I set different overlay.title to differentiate between the colors.

Unfortunately it always take the last set color and applies it to all polylines.

In my example below I have 3 polylines with title: "0", "1" and "2". The result is red stroke color for all polylines.

Does anyone has an idea how to fix this? Do you need more code?

Code to add title:

  func showAllRoutes(map: MKMapView, route: Routes) {
    var coordinates: [CLLocationCoordinate2D] = []
    
    
    if dm.show_all_routes {
        for x in route.routes.indices {
            for y in route.routes[x].route_numbers {
                coordinates.append(CLLocationCoordinate2D(latitude: dm.alle_staende.locations[y].latitude, longitude:dm.alle_staende.locations[y].longitude))
            }
            
            let overlay = MKPolyline(coordinates: coordinates, count: coordinates.count)
            overlay.subtitle = "Route"
            overlay.title = "\(x)"
            
            map.addOverlay(overlay)
            
        }
        
    }
}

答案1

得分: 0

I believe your If statement isn't executed. Your code seems right. I once did what you want to do, and the result, as you see, is what you want.

Swift MapKit多条带不同颜色的折线。

My code:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay is MKPolyline {
        let renderer = MKPolylineRenderer(overlay: overlay)
        renderer.strokeColor = .random()
        renderer.lineWidth = 0
        return renderer
    } else if overlay is MKPolygon {
        let renderer = MKPolygonRenderer(polygon: overlay as! MKPolygon)
        renderer.strokeColor = .random()
        renderer.fillColor = renderer.strokeColor?.withAlphaComponent(0.1)
        renderer.lineWidth = 2
        return renderer
    }
    return MKOverlayRenderer()
}

You better to check your code with this if statement:

if overlay_.title == "2" {
    renderer.strokeColor = UIColor.red
} else {
    renderer.strokeColor = UIColor.blue
}

To see if your code is okay or not, set a random color just to test it.

英文:

I believe your If statement isn't executed.your code seems right. I once did what you want to do, and the result as you see is what you want.

Swift MapKit多条带不同颜色的折线。

My code :

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolyline {
            let renderer = MKPolylineRenderer(overlay: overlay)
            renderer.strokeColor = .random()
            renderer.lineWidth = 0
            return renderer
            
        } else if overlay is MKPolygon {
            let renderer = MKPolygonRenderer(polygon: overlay as! MKPolygon)
            renderer.strokeColor = .random()
            
            renderer.fillColor = renderer.strokeColor?.withAlphaComponent(0.1)
            renderer.lineWidth = 2
            return renderer
        }
        
        return MKOverlayRenderer() 
}

You better to check your code with this if statement

if overlay_.title == "2" {

   renderer.strokeColor = UIColor.red

} else {

   renderer.strokeColor = UIColor.blue
}

to see if your code is ok or not, set a random color just to test it.

答案2

得分: 0

明白了。
问题是:

var coordinates: [CLLocationCoordinate2D] = []

它放错了位置,它添加了所有的线路,然后它们基本上都是相同的。这就是为什么它们具有相同的颜色。

func showAllRoutes(map: MKMapView, route: Routes) {
    
    if dm.show_all_routes {
        for x in route.routes.indices {

            var coordinates: [CLLocationCoordinate2D] = []

            for y in route.routes[x].route_numbers {
                coordinates.append(CLLocationCoordinate2D(latitude: dm.alle_staende.locations[y].latitude, longitude:dm.alle_staende.locations[y].longitude))
            }

            let overlay = MKPolyline(coordinates: coordinates, count: coordinates.count)
            overlay.subtitle = "Route"
            overlay.title = "\(x)"

            map.addOverlay(overlay)

        }
    }
}
英文:

Got it.
The problem is:

var coordinates: [CLLocationCoordinate2D] = []

It was placed wrong, it adds all lines and then they are basically the same. That's why they have the same color

 func showAllRoutes(map: MKMapView, route: Routes) {
    
    
    if dm.show_all_routes {
        for x in route.routes.indices {

            var coordinates: [CLLocationCoordinate2D] = []

            for y in route.routes[x].route_numbers {
                coordinates.append(CLLocationCoordinate2D(latitude: dm.alle_staende.locations[y].latitude, longitude:dm.alle_staende.locations[y].longitude))
            }
            
            let overlay = MKPolyline(coordinates: coordinates, count: coordinates.count)
            overlay.subtitle = "Route"
            overlay.title = "\(x)"
            
            map.addOverlay(overlay)
            
        }
        
    }
}

huangapple
  • 本文由 发表于 2023年2月26日 23:57:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定