Answered Jun 20, 2020 · 2 votes
See the documentation. You need to listen to the editing events for the polygon's path(s):
The listener must be set on the polygon's path. If the polygon has multiple paths, a listener must be set on each path.
Move the code that calculates the area into a function (calcArea) and call that on those events.
poly = new google.maps.Polygon({ strokeColor: '#f32222', strokeOpacity: 1, strokeWeight: 3, editable: true, map: map});google.maps.event.addListener(poly.getPath(), 'insert_at', calcArea);google.maps.event.addListener(poly.getPath(), 'set_at', calcArea);function calcArea() { var path = poly.getPath(); area = google.maps.geometry.spherical.computeArea(path); area = (Math.trunc(area)); document.getElementById("areaValue").value = area;}
When you clear the polygon, set its path back to an empty array, and re-add those event listeners:
document.getElementById("clearPoly").onclick = function() { area = 0; document.getElementById("areaValue").value = area; poly.setPath([]); path = poly.getPath(); google.maps.event.addListener(poly.getPath(), 'insert_at', calcArea); google.maps.event.addListener(poly.getPath(), 'set_at', calcArea);}
var poly;-function initMap() { var map = new google.maps.Map(document.getElementById('map'), { zoom: 9, mapTypeId: 'satellite', center: { lat: 52.486741, lng: -2.8055232 } }); var area = 0; // Create the search box and link it to the UI element. var input = document.getElementById('pac-input'); var searchBox = new google.maps.places.SearchBox(input); map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); // Bias the SearchBox results towards current map's viewport. map.addListener('bounds_changed', function() { searchBox.setBounds(map.getBounds()); }); var markers = []; // Listen for the event fired when the user selects a prediction and retrieve // more details for that place. searchBox.addListener('places_changed', function() { var places = searchBox.getPlaces(); if (places.length == 0) { return; } // Clear out the old markers. markers.forEach(function(marker) { marker.setMap(null); }); markers = []; // For each place, get the icon, name and location. var bounds = new google.maps.LatLngBounds(); places.forEach(function(place) { if (!place.geometry) { console.log("Returned place contains no geometry"); return; } var icon = { url: place.icon, size: new google.maps.Size(71, 71), origin: new google.maps.Point(0, 0), anchor: new google.maps.Point(17, 34), scaledSize: new google.maps.Size(25, 25) }; // Create a marker for each place. markers.push(new google.maps.Marker({ map: map, icon: icon, title: place.name, position: place.geometry.location })); if (place.geometry.viewport) { // Only geocodes have viewport. bounds.union(place.geometry.viewport); } else { bounds.extend(place.geometry.location); } }); map.fitBounds(bounds); }); poly = new google.maps.Polygon({ strokeColor: '#f32222', strokeOpacity: 1, strokeWeight: 3, editable: true, map: map }); google.maps.event.addListener(poly.getPath(), 'insert_at', calcArea); google.maps.event.addListener(poly.getPath(), 'set_at', calcArea); // Add a listener for the click event google.maps.event.addListener(map, 'click', function(event) { addLatLngToPoly(event.latLng, poly); }); document.getElementById("clearPoly").onclick = function() { area = 0; document.getElementById("areaValue").value = area; poly.setPath([]); path = poly.getPath(); google.maps.event.addListener(poly.getPath(), 'insert_at', calcArea); google.maps.event.addListener(poly.getPath(), 'set_at', calcArea); }}function addLatLngToPoly(latLng, poly) { var path = poly.getPath(); // Because path is an MVCArray, we can simply append a new coordinate // and it will automatically appear path.push(latLng); calcArea();}function calcArea() { var path = poly.getPath(); area = google.maps.geometry.spherical.computeArea(path); area = (Math.trunc(area)); document.getElementById("areaValue").value = area;}
#map { height: 100%;}html,body { height: 100%; margin: 0; padding: 0;}
<input id="pac-input" class="controls" type="text" placeholder="Search Box"><div id="map"></div><div id="right-panel"> <p>Area:</p> <p><textarea id="areaValue"></textarea>m</p> <button id="clearPoly">Clear</button></div><script src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry&callback=initMap" async defer></script>