TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

mapkit and renderer

Forums > SwiftUI

Hello,

I have two different overlays I would like to present on my map. However they always appear as one color for both sets of overlays...

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

        //parks
        if let drawnPolygon = overlay as? MKMultiPolygon{
            let renderer = MKMultiPolygonRenderer(multiPolygon: drawnPolygon)
            //renderer.strokeColor = UIColor.systemBlue
            renderer.lineWidth = 10
            renderer.fillColor = UIColor.systemRed.withAlphaComponent(0.5)
            return renderer
        }

            //====restricted zones
        if let drawnPolygons = overlay as? MKMultiPolygon{
            let renderer = MKMultiPolygonRenderer(multiPolygon: drawnPolygons)
            //renderer.strokeColor = UIColor.systemGreen
            renderer.lineWidth = 10
            renderer.fillColor = UIColor.black.withAlphaComponent(0.5)
            return renderer

            }

Is it because I am calling return renderer twice here? How should I proceed?

2      

Your assumption is somewhat correct. But basically the second if let is never going to be reached. As soon as the first if let closure runs, it will return from the function, and completely ignore any of the code after that.

The condition for both of your if let statements is basically the same. As long as overlay can be converted into an MKMultiPolygon and stored as a constant, then the first if let closure will be entered and you will return a result from your function. If it can not, then neither if let closure will be entered. So, the second if let closure is never entered.

2      

@Fly0strich

so how would I potentially combine these two so they both get executed?

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.