向MapKit添加多个圆圈

时间:2018-05-15 14:06:11

标签: ios mapkit mkoverlay mkcircle

继续我之前的解决question我面临一个新问题,所以我开始了一个新问题。

我试图为数组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)

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

1 个答案:

答案 0 :(得分:1)

终于找到了问题所在。 我正在重新实例化(rendererFor:)函数,所以我只是合并else if语句来涵盖可能的渲染器返回。事实上,我并不理解这样做是可能的。 所以最终的功能改为:

  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()
} 

希望这对别人有所帮助。