I'm building a React app which allows users to define plots through a Google Maps component by drawing overlay polygons. I'd like to push a GeoJSON up every time that the user draws a polygon.
The documentation is vague on this point. I'm using this sort of function:
onPolygonComplete={polygon => {
console.log(JSON.stringify(polygon.getPaths().b[0].b));
}}
...which produces this sort of thing:
[{"lat":32.22020791674245,"lng":35.22491455078125},{"lat":31.98754909816049,"lng":35.20294189453125},{"lat":32.0201569982896,"lng":35.43365478515625},{"lat":32.22485504316297,"lng":35.30731201171875}]
A good start-but is there anything built in that will produce an actual GeoJSON?
I can always use .map to go through this array and vivsect an GeoJSON object out of it...but wanted to ask if anyone knew of something already baked into the API.
In Google Earth, create the polygon that you wish to bring into RockWorks. Or, locate the polygon that currently exists in your Saved Places listing. Right-click on the item, and choose Copy from the pop-up menu. Or, right-click on the item, and choose Save Place As to save the locations in a KMZ file.
Google Maps Platform supports GeoJSON data with a single function call.
Loading data from the same domain The Google Maps Data Layer provides a container for arbitrary geospatial data (including GeoJSON). If your data is in a file hosted on the same domain as your Maps JavaScript API application, you can load it using the map. data. loadGeoJson() method.
Alright, I've got it figured out.
First, declare a GeoJSON template variable:
let GeoJSON = {
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: []
},
properties: {}
};
Then, in your drawing manager, do this:
onPolygonComplete={polygon => {
for (let point of polygon.getPath().getArray()) {
GeoJSON.geometry.coordinates.push([point.lng(), point.lat()]);
}
Now, you have a perfect GeoJSON polygon object. Voila!
Based on @Boris_K's answer, here is a more robust solution that:
google.maps.Polygon
.google.maps.Polygon.prototype.getGeoJSON = function() {
let geoJSON = {
type: "Polygon",
coordinates: []
};
let paths = this.getPaths().getArray();
for (path of paths) {
let pathArray = [];
let points = path.getArray();
let firstPoint = false;
for (point of points) {
if (firstPoint === false) {
firstPoint = point;
}
pathArray.push([point.lng(), point.lat()])
}
pathArray.push([firstPoint.lng(), firstPoint.lat()]);
geoJSON.coordinates.push(pathArray);
}
return geoJSON;
};
You can resort to conversion libraries such as Wicket to convert between google overlay objects and geojson valid literals. (Disclaimer: I've made a few contributions to Wicket in the past to suit my own use cases)
I made a fiddle showing this based on the example Simple Polygons
var triangleCoords = [
{lat: 25.774, lng: -80.190},
{lat: 18.466, lng: -66.118},
{lat: 32.321, lng: -64.757},
{lat: 25.774, lng: -80.190}
];
var bermudaTriangle = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
bermudaTriangle.setMap(map);
var wicket=new Wkt.Wkt(),
wktpolygon= wicket.fromObject(bermudaTriangle);
console.log(wktpolygon.toJson());
make sure to open the console to see the converted polygon
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With