Last active
March 6, 2024 21:35
-
-
Save Dygear/40e6e4770ece73955a267905e1ad8539 to your computer and use it in GitHub Desktop.
Google Maps JavaScript API getBounds on Polygon.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<main class="container"> | |
<div class="row" id="map" style="height: 600px"></div> | |
</main> | |
<script src="https://maps.googleapis.com/maps/api/js?key=<!--YOURKEYHERE-->&libraries=drawing"></script> | |
<script> | |
var map, drawingManager; | |
function initialize() { | |
map = new google.maps.Map(document.getElementById('map'), { | |
center: {lat: 40.9, lng: -73}, zoom: 10 | |
}); | |
drawingManager = new google.maps.drawing.DrawingManager({ | |
drawingControl: true, | |
drawingControlOptions: { | |
position: google.maps.ControlPosition.TOP_CENTER, | |
drawingModes: ['polygon', 'rectangle'] | |
}, | |
polygonOptions: { | |
editable: true, | |
} | |
}); | |
drawingManager.setMap(map); | |
google.maps.event.addListener(drawingManager, 'polygoncomplete', (polygon) => { | |
let bounds = new google.maps.LatLngBounds(); | |
map.data.forEach((polygon) => { | |
polygon.getGeometry().forEachLatLng((latlng) => { | |
bounds.extend(latlng); | |
}); | |
}); | |
let marker = new google.maps.Marker({ | |
position: bounds.getCenter(), | |
map: map, | |
title: mapGrid.value | |
}); | |
}); | |
} | |
google.maps.event.addDomListener(window, 'load', initialize); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<main class="container"> | |
<div class="row" id="map" style="height: 600px"></div> | |
</main> | |
<script src="https://maps.googleapis.com/maps/api/js?key=<!--YOURKEYHERE-->&libraries=drawing"></script> | |
<script> | |
var map, drawingManager; | |
function initialize() { | |
map = new google.maps.Map(document.getElementById('map'), { | |
center: {lat: 40.9, lng: -73}, zoom: 10 | |
}); | |
drawingManager = new google.maps.drawing.DrawingManager({ | |
drawingControl: true, | |
drawingControlOptions: { | |
position: google.maps.ControlPosition.TOP_CENTER, | |
drawingModes: ['polygon', 'rectangle'] | |
}, | |
polygonOptions: { | |
editable: true, | |
} | |
}); | |
drawingManager.setMap(map); | |
google.maps.event.addListener(drawingManager, 'polygoncomplete', (polygon) => { | |
let bounds = new google.maps.LatLngBounds(); | |
polygon.getPath().forEach((latLng) => { | |
bounds.extend(latLng); | |
}); | |
let marker = new google.maps.Marker({ | |
map: map, | |
title: mapGrid.value, | |
position: bounds.getCenter() | |
}); | |
}); | |
} | |
google.maps.event.addDomListener(window, 'load', initialize); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Draw a polygon with the DrawingManger. Once you complete the shape, it will fire the polygon complete method. It should then add a marker to the center of the polygon. But it does not.