Search code examples
iosmapkitmkpolygon

iOS MKPolygon pattern


I searched over the internet but I couldn't find an answer to this. Is possible to draw polygon filled with some pattern? I don't wanna use 3rd party libraries. I would like to achieve something similar to this: polygon with pattern


Solution

  • Yes you can do this.

    UIColor has an initializer for pattern images.

    guard let image = UIImage(named: "Name of the asset") else { return }
    let color = UIColor(patternImage: image)
    

    Then in your overlay renderer delegate method, you can simply assign it to your fill color.

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    
        // Renderer code
    
        yourRenderer.fillColor = color
    
        // Return your renderer
    
    }