Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to create a Donut with Javascript API V3(Empty space inside like a hole)

I want to create a hole in my Javascript Google API V3, so i follow Beginning Google Map API V3. But the code is rendering the whole area. Here is my Javascript code.

(function() {
    window.onload = function() {
        // Creating a map
        var options = {
            zoom: 6,
            center: new google.maps.LatLng(36.5, -79.8),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById('map'), options);

        // Creating an array with the points for the outer polygon
        var polyOuter = [
            new google.maps.LatLng(37.303, -81.256),
            new google.maps.LatLng(37.303, -78.333),
            new google.maps.LatLng(35.392, -78.333),
            new google.maps.LatLng(35.392, -81.256)
        ];

        // Creating an array with the points for the inner polygon
        var polyInner = [
            new google.maps.LatLng(36.705, -80.459),
            new google.maps.LatLng(36.705, -79),
            new google.maps.LatLng(35.9, -79),
            new google.maps.LatLng(35.9, -80.459)
        ];

        var points = [polyOuter, polyInner];

        // Creating the polygon
        var polygon = new google.maps.Polygon
        ({
            paths: points,
            map: map,
            strokeColor: '#ff0000',
            strokeOpacity: 0.6,
            strokeWeight: 3,
            fillColor: '#FF0000',
            fillOpacity: 0.35
        }); 
    };
})();
like image 820
Mohit Avatar asked Feb 18 '14 07:02

Mohit


1 Answers

One of the paths has to be reverted so polygons are drawn in different directions, for example:

var polyInner = [
    new google.maps.LatLng(35.9, -80.459),
    new google.maps.LatLng(35.9, -79),
    new google.maps.LatLng(36.705, -79),
    new google.maps.LatLng(36.705, -80.459)
];

My assumption is that the reason is how SVG or canvas render closed loops. If I am not wrong explanation lies in nonzero winding rule. See explanation at wikipedia.

Outer path is drawn clockwise, inner path is drawn counter-clockwise.

Set a counter to zero. Pick a point in object area and draw a line in direction out of object space. If the line cross clockwise path, add one. If the line cross counter-clockwise path segment, subtract one. If the final result for selected point is non-zero, the browser fills the area. If the final result is zero, the browser does not fill it.

So, if you pick up point in the 'hole', the result will be zero and area will not be filled.

like image 60
Anto Jurković Avatar answered Oct 21 '22 21:10

Anto Jurković