向 MapKit 添加多个圆

Adding multiple circles to MapKit

继续我之前解决的问题我遇到了一个新问题所以我开始了一个新问题。

我正在尝试为数组 var routeObsticle: [CLLocationCoordinate2D] = [] 中的每个 CLLocationCoordinates2D 添加圆圈,并且我有一个计数器 var routeObsticlePosition = 0,它在函数内部用于绕过数组的位置以获取值。 该数组在函数 checkAlerts()

中附加了它的值
func checkAlerts() {

        // MARK: checking if any notification is on our route
    while trackingCoordinatesArrayPosition != ( (trackingCoordinatesArray.count) - 1) {

        let trackingLatitude = trackingCoordinatesArray[trackingCoordinatesArrayPosition].latitude
        let trackingLongitude = trackingCoordinatesArray[trackingCoordinatesArrayPosition].longitude
        let alertLatitude = alertNotificationArray[alertNotificationArrayPosition].latitude
        let alertLongitude = alertNotificationArray[alertNotificationArrayPosition].longitude

        let coordinateFrom = CLLocation(latitude: trackingLatitude, longitude: trackingLongitude)
        let coordinateTo = CLLocation(latitude: alertLatitude, longitude: alertLongitude)
        let coordinatesDistanceInMeters = coordinateFrom.distance(from: coordinateTo)

//        if trackingLatitude == alertLatitude && trackingLongitude == alertLongitude {
            if coordinatesDistanceInMeters <= 10 {

            print( "found problem")
                routeObsticle.append(alertNotificationArray[alertNotificationArrayPosition])
            trackingCoordinatesArrayPosition = ( trackingCoordinatesArrayPosition + 1)

        }
        else if alertNotificationArrayPosition < ((alertNotificationArray.count) - 1) {

            alertNotificationArrayPosition = alertNotificationArrayPosition + 1
            }
        else if  alertNotificationArrayPosition == (alertNotificationArray.count - 1) {

            trackingCoordinatesArrayPosition = ( trackingCoordinatesArrayPosition + 1)
            alertNotificationArrayPosition = 0
           }

       }
}

但是我没有为每个数组的条目画一个圆圈。

函数如下:

func displayObsticles() {

    while routeObsticlePosition <= ((routeObsticle.count) - 1) {
        let obsticleLatitude = routeObsticle[routeObsticlePosition].latitude
        let obsticleLongitude = routeObsticle[routeObsticlePosition].longitude
        let title = ""
        let circleCoordinates = CLLocationCoordinate2DMake(obsticleLatitude, obsticleLongitude)
        let regionRadious = 10.0

//            let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: obsticleLatitude, longitude: obsticleLatitude), radius: regionRadious, identifier: title)

        let circle = MKCircle(center: (routeObsticle[routeObsticlePosition]), radius: regionRadious)
        mapView.add(circle)
        routeObsticlePosition = ((routeObsticlePosition) + 1)

    }
    if routeObsticlePosition == ((routeObsticle.count) - 1) {
        return
    }
} 

然后我有了 MKCircleRenderer 方法:

 func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer{            

    let circleRenderer = MKCircleRenderer(overlay: overlay)
    circleRenderer.strokeColor = UIColor.red
    circleRenderer.lineWidth = 1.0
    return circleRenderer            
}

控制台消息如下:

2018-05-15 15:45:01.056042+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10554.25436.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.056198+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10555.25436.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.056318+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10552.25436.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.056429+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10553.25436.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.056561+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10551.25436.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.056698+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10555.25434.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.056843+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10555.25435.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.056977+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10554.25435.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.057120+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10553.25435.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.057257+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10552.25435.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.057391+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10551.25435.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.057531+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10555.25432.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.057643+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10555.25433.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.057772+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10554.25430.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.057987+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10555.25430.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.058244+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10555.25431.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.058450+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10552.25430.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.058685+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10553.25430.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.058939+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10551.25430.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.059208+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10555.25429.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.059478+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10554.25429.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.059718+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10553.25429.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.059979+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10552.25429.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:01.060211+0200 fix-it mapView[3541:172723] [VKDefault] Tile 10551.25429.16 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:02.424614+0200 fix-it mapView[3541:172723] [VKDefault] Tile 42214.101729.18 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:02.424753+0200 fix-it mapView[3541:172723] [VKDefault] Tile 42213.101729.18 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:02.424899+0200 fix-it mapView[3541:172723] [VKDefault] Tile 42212.101729.18 (128) in current unloaded state for 0.00 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.00 sec)
2018-05-15 15:45:12.513053+0200 fix-it mapView[3541:172723] [VKDefault] Tile 42214.101729.18 (128) in current unloaded state for 10.09 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (10.09 sec)
2018-05-15 15:45:12.513209+0200 fix-it mapView[3541:172723] [VKDefault] Tile 42213.101729.18 (128) in current unloaded state for 10.09 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (10.09 sec)
2018-05-15 15:45:12.513319+0200 fix-it mapView[3541:172723] [VKDefault] Tile 42212.101729.18 (128) in current unloaded state for 10.09 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (10.09 sec)

可以这样绘制多个圆圈吗?或者我应该如何处理这个任务? 提前谢谢了。我从叠加开始,当然我确实误解了一些东西。

终于找到问题出在哪里了。 我正在重新实例化 (rendererFor:) 函数,所以我只是合并了 else if 语句来覆盖 return 可能的渲染器。事实上,我不明白这样做是可能的。 所以最后的函数改为:

  func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay is MKCircle {
    let renderer = MKCircleRenderer(overlay: overlay)
    renderer.fillColor = UIColor.black.withAlphaComponent(0.1)
    renderer.strokeColor = UIColor.blue
    renderer.lineWidth = 2
    return renderer

    } else if overlay is MKPolyline {
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = UIColor.orange
    renderer.lineWidth = 3
    return renderer

    } else if overlay is MKPolygon {
    let renderer = MKPolygonRenderer(polygon: overlay as! MKPolygon)
    renderer.fillColor = UIColor.black.withAlphaComponent(0.5)
    renderer.strokeColor = UIColor.orange
    renderer.lineWidth = 2
    return renderer
    }

    return MKOverlayRenderer()
} 

希望这对其他人有所帮助。