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

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

Swift MapKit Multiple Polylines with different colors

问题

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

  1. func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
  2. if let overlay_ = overlay as? MKPolyline {
  3. let renderer = MKPolylineRenderer(overlay: overlay)
  4. if overlay_.title == "2" {
  5. renderer.strokeColor = UIColor.red
  6. } else {
  7. renderer.strokeColor = UIColor.blue
  8. }
  9. renderer.lineWidth = 5
  10. return renderer
  11. } else {
  12. return MKOverlayRenderer()
  13. }
  14. }

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

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

  1. func showAllRoutes(map: MKMapView, route: Routes) {
  2. var coordinates: [CLLocationCoordinate2D] = []
  3. if dm.show_all_routes {
  4. for x in route.routes.indices {
  5. for y in route.routes[x].route_numbers {
  6. coordinates.append(CLLocationCoordinate2D(latitude: dm.alle_staende.locations[y].latitude, longitude: dm.alle_staende.locations[y].longitude))
  7. }
  8. let overlay = MKPolyline(coordinates: coordinates, count: coordinates.count)
  9. overlay.subtitle = "Route"
  10. overlay.title = "\(x)"
  11. map.addOverlay(overlay)
  12. }
  13. }
  14. }
英文:

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

  1. func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
  2. if let overlay_ = overlay as? MKPolyline {
  3. let renderer = MKPolylineRenderer(overlay: overlay)
  4. if overlay_.title == "2" {
  5. renderer.strokeColor = UIColor.red
  6. } else {
  7. renderer.strokeColor = UIColor.blue
  8. }
  9. renderer.lineWidth = 5
  10. return renderer
  11. } else {
  12. return MKOverlayRenderer()
  13. }
  14. }

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:

  1. func showAllRoutes(map: MKMapView, route: Routes) {
  2. var coordinates: [CLLocationCoordinate2D] = []
  3. if dm.show_all_routes {
  4. for x in route.routes.indices {
  5. for y in route.routes[x].route_numbers {
  6. coordinates.append(CLLocationCoordinate2D(latitude: dm.alle_staende.locations[y].latitude, longitude:dm.alle_staende.locations[y].longitude))
  7. }
  8. let overlay = MKPolyline(coordinates: coordinates, count: coordinates.count)
  9. overlay.subtitle = "Route"
  10. overlay.title = "\(x)"
  11. map.addOverlay(overlay)
  12. }
  13. }
  14. }

答案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:

  1. func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
  2. if overlay is MKPolyline {
  3. let renderer = MKPolylineRenderer(overlay: overlay)
  4. renderer.strokeColor = .random()
  5. renderer.lineWidth = 0
  6. return renderer
  7. } else if overlay is MKPolygon {
  8. let renderer = MKPolygonRenderer(polygon: overlay as! MKPolygon)
  9. renderer.strokeColor = .random()
  10. renderer.fillColor = renderer.strokeColor?.withAlphaComponent(0.1)
  11. renderer.lineWidth = 2
  12. return renderer
  13. }
  14. return MKOverlayRenderer()
  15. }

You better to check your code with this if statement:

  1. if overlay_.title == "2" {
  2. renderer.strokeColor = UIColor.red
  3. } else {
  4. renderer.strokeColor = UIColor.blue
  5. }

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 :

  1. func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
  2. if overlay is MKPolyline {
  3. let renderer = MKPolylineRenderer(overlay: overlay)
  4. renderer.strokeColor = .random()
  5. renderer.lineWidth = 0
  6. return renderer
  7. } else if overlay is MKPolygon {
  8. let renderer = MKPolygonRenderer(polygon: overlay as! MKPolygon)
  9. renderer.strokeColor = .random()
  10. renderer.fillColor = renderer.strokeColor?.withAlphaComponent(0.1)
  11. renderer.lineWidth = 2
  12. return renderer
  13. }
  14. return MKOverlayRenderer()
  15. }

You better to check your code with this if statement

  1. if overlay_.title == "2" {
  2. renderer.strokeColor = UIColor.red
  3. } else {
  4. renderer.strokeColor = UIColor.blue
  5. }

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

答案2

得分: 0

明白了。
问题是:

  1. var coordinates: [CLLocationCoordinate2D] = []

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

  1. func showAllRoutes(map: MKMapView, route: Routes) {
  2. if dm.show_all_routes {
  3. for x in route.routes.indices {
  4. var coordinates: [CLLocationCoordinate2D] = []
  5. for y in route.routes[x].route_numbers {
  6. coordinates.append(CLLocationCoordinate2D(latitude: dm.alle_staende.locations[y].latitude, longitude:dm.alle_staende.locations[y].longitude))
  7. }
  8. let overlay = MKPolyline(coordinates: coordinates, count: coordinates.count)
  9. overlay.subtitle = "Route"
  10. overlay.title = "\(x)"
  11. map.addOverlay(overlay)
  12. }
  13. }
  14. }
英文:

Got it.
The problem is:

  1. 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

  1. func showAllRoutes(map: MKMapView, route: Routes) {
  2. if dm.show_all_routes {
  3. for x in route.routes.indices {
  4. var coordinates: [CLLocationCoordinate2D] = []
  5. for y in route.routes[x].route_numbers {
  6. coordinates.append(CLLocationCoordinate2D(latitude: dm.alle_staende.locations[y].latitude, longitude:dm.alle_staende.locations[y].longitude))
  7. }
  8. let overlay = MKPolyline(coordinates: coordinates, count: coordinates.count)
  9. overlay.subtitle = "Route"
  10. overlay.title = "\(x)"
  11. map.addOverlay(overlay)
  12. }
  13. }
  14. }

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:

确定