Is there built-in support or any library to export geoJSON
data from the google.maps.Data
layer or google.maps.Data.Feature
or googl
None of these worked for me. I figured out a way to export custom Polygons which may be useful for other shapes as well.
Here's the key export function:
function getPathArray(polygon) {
return polygon.getPath().getArray().map(p => {
return { lat: p.lat(), lng: p.lng() }
})
}
Here's a full example:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: { lat: 25.774, lng: -70.190 }, // bermuda triangle
});
const bermudaTriangle = new google.maps.Polygon({
paths: [
{ lat: 25.774, lng: -80.190 },
{ lat: 18.466, lng: -66.118 },
{ lat: 32.321, lng: -64.757 },
],
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
editable: true,
draggable: false
});
bermudaTriangle.setMap(map);
bermudaTriangle.getPaths().forEach(function (path, index) {
google.maps.event.addListener(path, 'insert_at', function () {
var data = getPathArray(bermudaTriangle)
console.log(JSON.stringify(data))
})
google.maps.event.addListener(path, 'remove_at', function () {
var data = getPathArray(bermudaTriangle)
console.log(JSON.stringify(data))
})
google.maps.event.addListener(path, 'set_at', function () {
var data = getPathArray(bermudaTriangle)
console.log(JSON.stringify(data))
})
})
google.maps.event.addListener(bermudaTriangle, 'dragend', function () {
console.log("dragged")
})
}
function getPathArray(polygon) {
return polygon.getPath().getArray().map(p => {
return { lat: p.lat(), lng: p.lng() }
})
}
Then use the json that gets printed to the console and import it
bermudaTriangle.setPath(JSON.parse(myJson))