Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Although I didn't use canvas, how about this code?</p> <pre><code>function onPolygonComplete(polygon) { var bounds, paths, sw, ne, ystep, xstep, boxH, boxW, posArry, flag, pos, x, y, i, box, maxBoxCnt; //Delete old boxes. boxes.forEach(function(box, i) { box.setMap(null); delete box; }); //Calculate the bounds that contains entire polygon. bounds = new google.maps.LatLngBounds(); paths = polygon.getPath(); paths.forEach(function(latlng, i){ bounds.extend(latlng); }); //Calculate the small box size. maxBoxCnt = 8; sw = bounds.getSouthWest(); ne = bounds.getNorthEast(); ystep = Math.abs(sw.lat() - ne.lat()) / maxBoxCnt; boxH = Math.abs(sw.lat() - ne.lat()) / (maxBoxCnt + 1); xstep = Math.abs(sw.lng() - ne.lng()) / maxBoxCnt; boxW = Math.abs(sw.lng() - ne.lng()) / (maxBoxCnt + 1); for (y = 0; y &lt; maxBoxCnt; y++) { for (x = 0; x &lt; maxBoxCnt; x++) { //Detect that polygon is able to contain a small box. bounds = new google.maps.LatLngBounds(); posArry = []; posArry.push(new google.maps.LatLng(sw.lat() + ystep * y, sw.lng() + xstep * x)); posArry.push(new google.maps.LatLng(sw.lat() + ystep * y, sw.lng() + xstep * x + boxW)); posArry.push(new google.maps.LatLng(sw.lat() + ystep * y + boxH, sw.lng() + xstep * x)); posArry.push(new google.maps.LatLng(sw.lat() + ystep * y + boxH, sw.lng() + xstep * x + boxW)); flag = true; for (i = 0; i &lt; posArry.length; i++) { pos = posArry[i]; if (flag) { flag = google.maps.geometry.poly.containsLocation(pos, polygon); bounds.extend(pos); } } //Draw a small box. if (flag) { box = new google.maps.Rectangle({ bounds : bounds, map : mapCanvas, strokeColor: '#00ffff', strokeOpacity: 0.5, strokeWeight: 1, fillColor: '#00ffff', fillOpacity : 0.5, clickable: false }); boxes.push(box); } } } } </code></pre> <p>This code works like this image. <img src="https://i.stack.imgur.com/m6c0M.png" alt="enter image description here"></p> <p>I wrote a page that explains the code. <a href="http://googlemaps.googlermania.com/google_maps_api_v3/en/poly_containsLocation.html" rel="noreferrer">http://googlemaps.googlermania.com/google_maps_api_v3/en/poly_containsLocation.html</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload