Drawing Tools
Sample code for Google Maps Platform JavaScript API
author(s):
Justin Poehnelt
HTML
<!DOCTYPE html>
<!--
@license
Copyright 2019 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<html>
<head>
<title>Drawing Tools</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="map"></div>
<!--
The `defer` attribute causes the callback to execute after the full HTML
document has been parsed. For non-blocking uses, avoiding race conditions,
and consistent behavior across browsers, consider loading using Promises.
See https://developers.google.com/maps/documentation/javascript/load-maps-js-api
for more information.
-->
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&libraries=drawing,geometry&v=weekly"
defer
></script>
</body>
</html>
CSS
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Always set the map height explicitly to define the size of the div element
* that contains the map.
*/
#map {
height: 100%;
}
/*
* Optional: Makes the sample page fill the window.
*/
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// This example requires the Drawing library. Include the libraries=drawing
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=drawing">
let startLatLng;
let currentMovingPolyIndex = -1;
const polygonsInstance = [];
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
center: {
lat: -34.397,
lng: 150.644
},
zoom: 18,
});
const drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.POLYGON,
],
},
});
drawingManager.setMap(map);
// ==================================================== //
const onDrawingComplete = (polygon) => {
polygon.setOptions({
draggable: true,
clickable: true
});
polygonsInstance.push(polygon);
polygon.index = polygonsInstance.length - 1;
polygon.addListener('dragstart', ({
latLng
}) => {
currentMovingPolyIndex = polygon.index;
startLatLng = latLng;
})
polygon.addListener('drag', ({
latLng: endLatLng
}) => {
const heading = google.maps.geometry.spherical.computeHeading(startLatLng, endLatLng);
const movedDistance = google.maps.geometry.spherical.computeDistanceBetween(startLatLng, endLatLng);
polygonsInstance.forEach((polygonInst, index) => {
if (currentMovingPolyIndex !== index) {
const currentPath = polygonInst.getPath().getArray();
const newPath = currentPath.map(path => {
return google.maps.geometry.spherical.computeOffset(path, movedDistance, heading);
});
...