Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is some code (for the Google Maps JavaScript API version 3) that achieves what you want. It supports:</p> <ul> <li>clicking to append vertices.</li> <li>clicking on the first vertex again to close the path.</li> <li>dragging vertices.</li> </ul> <p>It's undocumented, but hopefully you can see what it's doing easily enough.</p> <pre><code>$(document).ready(function () { var map = new google.maps.Map(document.getElementById('map'), { center: new google.maps.LatLng(21.17, -86.66), zoom: 9, mapTypeId: google.maps.MapTypeId.HYBRID, scaleControl: true }); var isClosed = false; var poly = new google.maps.Polyline({ map: map, path: [], strokeColor: "#FF0000", strokeOpacity: 1.0, strokeWeight: 2 }); google.maps.event.addListener(map, 'click', function (clickEvent) { if (isClosed) return; var markerIndex = poly.getPath().length; var isFirstMarker = markerIndex === 0; var marker = new google.maps.Marker({ map: map, position: clickEvent.latLng, draggable: true }); if (isFirstMarker) { google.maps.event.addListener(marker, 'click', function () { if (isClosed) return; var path = poly.getPath(); poly.setMap(null); poly = new google.maps.Polygon({ map: map, path: path, strokeColor: "#FF0000", strokeOpacity: 0.8, strokeWeight: 2, fillColor: "#FF0000", fillOpacity: 0.35 }); isClosed = true; }); } google.maps.event.addListener(marker, 'drag', function (dragEvent) { poly.getPath().setAt(markerIndex, dragEvent.latLng); }); poly.getPath().push(clickEvent.latLng); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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