从Google地图导出geoJSON数据

vam*_*olu 5 javascript export geojson google-maps-api-3

是否有内置的支持或出口任何图书馆geoJSON从数据google.maps.Data或层google.maps.Data.Feature或者google.maps.Data.Geometry甚至使用Marker,PolylinePolygon.我有这样的代码,例如:

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

我想将这些数据作为geojson导出到服务器.像toGeoJson方法一样leaflet

Dr.*_*lle 7

样本函数:

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;
}
Run Code Online (Sandbox Code Playgroud)

该函数使用数据创建一个对象.您可以将回调作为参数传递,该参数将以对象作为参数执行.

样品呼叫:

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

演示:http://jsfiddle.net/doktormolle/5F88D/

注意:演示也会存储圈子,但GeoJSON不支持圈子.作为一种解决方法,它将圆圈存储为具有radius-property的POINT.

当具有radius-property的POINT将被加载到数据层时,demo将隐藏标记,并根据几何和radius-property创建一个圆.


<edit>:现在有一个可用于geoJSON导出的内置方法: google.maps.Data.toGeoJson()

有关示例,请参阅Google地图以外的保存地图实例