Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverted Polygon in GMSMapView

I have to use Google Map for my iPhone project and I'am using GMSPolygon to draw a polygon but How can I fill everywhere on my map except the interior of the polygon. Like the image below.

Thank You.

sample

like image 472
ethem Avatar asked Dec 25 '13 11:12

ethem


1 Answers

I have played with your question. Main idea is to fill all Earth with polygon and then make holes for your particular area. Here is example code, but problem is that I can`t create polygon to cover whole Earth. First approach is to create 4 polygons for each quarter.

PGeoCountry is a struct with desired areas in boundariesPaths:[GMSPath]

func locateCountry(country: PGeoCountry) {
    // 1. Create one quarter earth filling polygon
    let fillingPath = GMSMutablePath()
    fillingPath.addLatitude(90.0, longitude: -90.0)
    fillingPath.addLatitude(90.0, longitude: 90.0)
    fillingPath.addLatitude(0, longitude: 90.0)
    fillingPath.addLatitude(0, longitude: -90.0)

    let fillingPolygon = GMSPolygon(path:fillingPath)
    let fillColor = UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 0.6)
    fillingPolygon.fillColor = fillColor
    fillingPolygon.map = self.mapView

    // 2. Add prepared array of GMSPath
    fillingPolygon.holes = country.boundariesPaths

    // 3. Add lines for boundaries
    for path in country.boundariesPaths {
        let line = GMSPolyline(path: path)
        line.map = self.mapView
        line.strokeColor = UIColor.blackColor()
    }

}

Here is the result:

enter image description here

like image 181
Dren Avatar answered Nov 01 '22 12:11

Dren