Skip to content

Instantly share code, notes, and snippets.

@emoran
Last active October 15, 2020 13:42
Show Gist options
  • Save emoran/076b526d6bd4297544f46a8d3dba7965 to your computer and use it in GitHub Desktop.
Save emoran/076b526d6bd4297544f46a8d3dba7965 to your computer and use it in GitHub Desktop.
Adding polygone in google maps
<!DOCTYPE html>
<html>
<head>
<title>Drawing tools</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 80%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<input id="save" value="save" type="button" />
<div id="geojson"></div>
</body>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 40.7328822, lng: -74.0006213},
zoom: 12
});
/*map.data.setControls(['Polygon']);
map.data.setStyle({
editable: true,
draggable: true
});*/
//bindDataLayerListeners(map.data);
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: ['polygon']
}
});
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
switch (event.type) {
case google.maps.drawing.OverlayType.MARKER:
map.data.add(new google.maps.Data.Feature({
geometry: new google.maps.Data.Point(event.overlay.getPosition())
}));
break;
case google.maps.drawing.OverlayType.RECTANGLE:
var b = event.overlay.getBounds(),
p = [b.getSouthWest(), {
lat: b.getSouthWest().lat(),
lng: b.getNorthEast().lng()
},
b.getNorthEast(), {
lng: b.getSouthWest().lng(),
lat: b.getNorthEast().lat()
}
]
map.data.add(new google.maps.Data.Feature({
geometry: new google.maps.Data.Polygon([p])
}));
break;
case google.maps.drawing.OverlayType.POLYGON:
map.data.add(new google.maps.Data.Feature({
geometry: new google.maps.Data.Polygon([event.overlay.getPath().getArray()])
}));
break;
case google.maps.drawing.OverlayType.POLYLINE:
map.data.add(new google.maps.Data.Feature({
geometry: new google.maps.Data.LineString(event.overlay.getPath().getArray())
}));
break;
case google.maps.drawing.OverlayType.CIRCLE:
map.data.add(new google.maps.Data.Feature({
properties: {
radius: event.overlay.getRadius()
},
geometry: new google.maps.Data.Point(event.overlay.getCenter())
}));
break;
}
});
google.maps.event.addDomListener(document.getElementById('save'), 'click', function() {
map.data.toGeoJson(function(obj) {
document.getElementById('geojson').innerHTML = JSON.stringify(obj);
});
})
loadPolygons(map);
}
// Apply listeners to refresh the GeoJson display on a given data layer.
function bindDataLayerListeners(dataLayer) {
dataLayer.addListener('addfeature', savePolygon);
dataLayer.addListener('removefeature', savePolygon);
dataLayer.addListener('setgeometry', savePolygon);
}
function loadPolygons(map) {
var data = JSON.parse('{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-74.10690307617188,40.75531957477602],[-73.98571014404297,40.74023389131268],[-74.04373168945312,40.67829474034605],[-74.12063598632812,40.69183258262447],[-74.10690307617188,40.75531957477602]]]},"properties":{}}]}');
map.data.forEach(function (f) {
map.data.remove(f);
});
map.data.addGeoJson(data)
}
function savePolygon() {
map.data.toGeoJson(function (json) {
//localStorage.setItem('geoData', JSON.stringify(json));
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&libraries=drawing&callback=initMap"></script>
</html>
@answerquest
Copy link

Thanks for sharing this, the Feature snippet was very useful.

@richiwarmen
Copy link

Hi @emoran, you have to remove your google maps key from the example.

@emoran
Copy link
Author

emoran commented Oct 15, 2020

Thanks @richiwarmen just updated the snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment