How to invert KML so that area outside of polygon

2019-07-20 12:13发布

问题:

I have a KML File which is a polygon that overlays the boundaries of a city. Currently my polygon is shaded gray. I would like to invert it, so the rest of the world is greyed out,

Here is a link to the kml, it should take you to Google maps. City of Edmonton

I'm not really sure if this is something I need to change in my KML, or can be accomplished with the Google Maps API.

回答1:

You need to change your KML. Add an outer boundary to the polygon that covers the whole world. Make sure the winding direction of the outer polygon is opposite the winding direction of the inner polygon.

example (state of Virginia).

Note that KML doesn't seem to work with the Google Maps Javascript API v3 KmlLayer renderer, might need to reverse the winding directions.



回答2:

Now google maps provide method addGeoJson to the map so it's better to convert KML to geojson you can this tool for this step https://mapbox.github.io/togeojson/

Then in the coordinates array add this array as a first input in coordinates

[
                    [0, 90],
                    [180, 90],
                    [180, -90],
                    [0, -90],
                    [-180, -90],
                    [-180, 0],
                    [-180, 90],
                    [0, 90]
                ]

It'll do the inversions and you can find examples here https://github.com/minaalfy/city-map