Export geoJSON data from Google Maps

前端 未结 3 537
灰色年华
灰色年华 2021-02-03 14:41

Is there built-in support or any library to export geoJSON data from the google.maps.Data layer or google.maps.Data.Feature or googl

3条回答
  •  南方客
    南方客 (楼主)
    2021-02-03 15:10

    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

    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.


    : 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

提交回复
热议问题