iosmapkitmkoverlaymkcircle

Adding multiple circles to MapKit


Continuing from my previous solved question I'm facing a new problem so I started a new question.

I'm trying to add circle for each CLLocationCoordinates2D in an array var routeObsticle: [CLLocationCoordinate2D] = [] and I have a counter var routeObsticlePosition = 0 that inside a function is used to circle through the array's positions to get a value. The array gets it's values appended in the function 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
           }

       }
}

But I fail to draw a circle for each array's entry.

Here's the function:

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

then I have the MKCircleRenderer method :

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

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

Here's the console messages:

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)

Can drawing multiple circles be done like this or how should I approach the task? many thanks in advance. I'm starting with overlays and surely I did misunderstand something.


Solution

  • finally found out where was the problem. I was re-instantiating the (rendererFor:) function, so I just incorporated else if statements to cover the possible renderers to return. Indeed I didn't understand it was possible to do it that way. So the final function changed to :

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

    Hope that this will be a help to others.