Search code examples
javascriptgoogle-mapsgeojson

Google maps get geojson data


How to get current marker position as geojson?

I have working google maps code but i need to transform google maps latLng coordinates format to geojson

submitHandler(e) {
    e.preventDefault();
    let geojson = getGeoJsonSomehow(this.marker.getPosition());
    this.updateData({location: geojson}).then(...).catch(this.setError)
}

Type of geometry - Point

Geojson format: http://geojson.org/


Solution

  • You can get the geoJson of the marker by creating the Point feature as below.

    submitHandler(e) {
        //Your code
        var point = new google.maps.Data.Feature({
            geometry : new google.maps.Data.Point(this.marker.getPosition())
        });
        var geoJson = "";
        point.toGeoJson(function(json) {
            geoJson = JSON.stringify(json)
            console.log(geoJson);
        });
        //geoJson variable will have the required json string.
        //Your code   
    }