从Google地图导出geoJSON数据

从Google地图导出geoJSON数据

本文介绍了从Google地图导出geoJSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有内置支持或任何可从 google.maps.Data 层导出 geoJSON 数据的库或 google.maps.Data.Feature google.maps.Data.Geometry 甚至使用标记折线多边形。我有这样的代码,例如: / p>

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);

我想将这些数据作为geojson导出到服务器上。像$ code > toGeoJson 方法在传单

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

推荐答案

一个示例函数:

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.

示例调用:

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

演示:

注意: / strong>演示也存储圈子,但GeoJSON不支持圈子。作为一种解决方法,它将圆圈存储为具有半径属性的点。

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.

当将具有radius属性的POINT加载到数据层时,演示隐藏标记并创建一个基于几何和半径属性的圆。

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> :现在有一个可用于geoJSON导出的内置方法:

请参阅 为例

See Save Map Instance outside of Google Maps for an example

这篇关于从Google地图导出geoJSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:57