UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.