Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export geoJSON data from Google Maps

Is there built-in support or any library to export geoJSON data from the google.maps.Data layer or google.maps.Data.Feature or google.maps.Data.Geometry or even using Marker,Polyline and Polygon.I have code like this,for example:

 var point=new google.maps.Data.Point(m.getPosition());
 activeFeature.setGeometry(point);
 console.log(activeFeature.getGeometry());
 equiLayer.add(activeFeature);

I would like to export this data to the server as geojson.Something like the toGeoJson method in leaflet?

like image 599
vamsiampolu Avatar asked Aug 01 '14 02:08

vamsiampolu


People also ask

Does Google Maps use GeoJSON?

Google Maps Platform supports GeoJSON data with a single function call.

How do I export a GeoJSON File?

To export data, click on Special – Export to GeoJSON. Clicking on this item will display the dialog as in the following figure. In this dialog, you can setup the GeoJSON export. You can choose from two export modes – Selected Layer of All Visible Layers.

How do I import GeoJSON into Google Maps?

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.


1 Answers

A sample-function:

google.maps.Map.prototype.getGeoJson=function(callback){
  var geo={"type": "FeatureCollection","features": []},
      fx=function(g,t){

        var that  =[],
            arr,
            f     = {
                      MultiLineString :'LineString',
                      LineString      :'Point',
                      MultiPolygon    :'Polygon',
                      Polygon         :'LinearRing',
                      LinearRing      :'Point',
                      MultiPoint      :'Point'
                    };

        switch(t){
          case 'Point':
            g=(g.get)?g.get():g;
            return([g.lng(),g.lat()]);
            break;
          default:
            arr= g.getArray();
            for(var i=0;i<arr.length;++i){
              that.push(fx(arr[i],f[t]));
            }
            if( t=='LinearRing' 
                  &&
                that[0]!==that[that.length-1]){
              that.push([that[0][0],that[0][1]]);
            }
            return that;
        }
      };

  this.data.forEach(function(feature){
   var _feature     = {type:'Feature',properties:{}}
       _id          = feature.getId(),
       _geometry    = feature.getGeometry(),
       _type        =_geometry.getType(),
       _coordinates = fx(_geometry,_type);

       _feature.geometry={type:_type,coordinates:_coordinates};
       if(typeof _id==='string'){
        _feature.id=_id;
       }

       geo.features.push(_feature);
       feature.forEachProperty(function(v,k){
          _feature.properties[k]=v;
       });
  }); 
  if(typeof callback==='function'){
    callback(geo);
  }     
  return geo;
}

The function creates an object with the data. You may pass a callback as argument which will be executed with the object as argument.

Sample-call:

//map is the google.maps.Map-instance
map.getGeoJson(function(o){console.log(o)});

Demo: http://jsfiddle.net/doktormolle/5F88D/

Note: the Demo also stores circles, but circles are not supported in GeoJSON. As a workaround it stores circles as a POINT with a radius-property.

When a POINT with a radius-property will be loaded into the data-layer the demo hides the marker and creates a circle based on geometry and the radius-property instead.


<edit>: there is now a built-in method available for geoJSON-export: google.maps.Data.toGeoJson()

See Save Map Instance outside of Google Maps for an example

like image 159
Dr.Molle Avatar answered Oct 09 '22 08:10

Dr.Molle