从Google Maps导出geoJSON数据
Export geoJSON data from Google Maps
是否有内置支持或任何库从google.maps.Data
层或google.maps.Data.Feature
或google.maps.Data.Geometry
甚至使用Marker
, Polyline
和Polygon
导出geoJSON
数据。我有这样的代码,例如:
var point=new google.maps.Data.Point(m.getPosition());
activeFeature.setGeometry(point);
console.log(activeFeature.getGeometry());
equiLayer.add(activeFeature);
我想将此数据导出到服务器作为geojson。类似于leaflet
?
toGeoJson
方法函数示例:
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;
}
函数用数据创建一个对象。你可以传递一个回调函数作为参数,它将以对象作为参数执行。
示例调用:
//map is the google.maps.Map-instance
map.getGeoJson(function(o){console.log(o)});
演示: http://jsfiddle.net/doktormolle/5F88D/
注意: Demo也存储圆圈,但圆圈在GeoJSON中不支持。作为一种解决方法,它将圆存储为具有半径属性的点。
当一个带有radius属性的POINT被加载到数据层时,演示将隐藏标记并创建一个基于几何形状和radius属性的圆。
<edit>
:现在有一个内置的方法可用于geoJSON-export: google.maps.Data.toGeoJson()
参见Save Map Instance outside of Google Maps示例
嗨,我想分享我所做的从谷歌地图导出geojson数据到html文本区域。
这是我的html视图
<article id="article3" style="">
<div style="margin: 2px 0px 2px 0px;">
<button onclick= "exportData()">Save</button>
</div>
<textarea id="geojson-input" placeholder="..." class="" style="height: 97%; width: 100%"></textarea>
</article>
这是我的javascript
map.data.toGeoJson(function (data) {
document.querySelector('#geojson-input').innerHTML = JSON.stringify(data);
});
函数geojson是map的函数。Data类,将从映射中获取所有数据并返回对象。要在文本区域上显示该对象,我必须使用JSON.stringify(data);
将该对象转换为字符串希望对你有帮助!
这些都不适合我。我想出了一种导出自定义多边形的方法,这可能对其他形状也很有用。
下面是关键的导出函数:
function getPathArray(polygon) {
return polygon.getPath().getArray().map(p => {
return { lat: p.lat(), lng: p.lng() }
})
}
下面是一个完整的例子:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: { lat: 25.774, lng: -70.190 }, // bermuda triangle
});
const bermudaTriangle = new google.maps.Polygon({
paths: [
{ lat: 25.774, lng: -80.190 },
{ lat: 18.466, lng: -66.118 },
{ lat: 32.321, lng: -64.757 },
],
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
editable: true,
draggable: false
});
bermudaTriangle.setMap(map);
bermudaTriangle.getPaths().forEach(function (path, index) {
google.maps.event.addListener(path, 'insert_at', function () {
var data = getPathArray(bermudaTriangle)
console.log(JSON.stringify(data))
})
google.maps.event.addListener(path, 'remove_at', function () {
var data = getPathArray(bermudaTriangle)
console.log(JSON.stringify(data))
})
google.maps.event.addListener(path, 'set_at', function () {
var data = getPathArray(bermudaTriangle)
console.log(JSON.stringify(data))
})
})
google.maps.event.addListener(bermudaTriangle, 'dragend', function () {
console.log("dragged")
})
}
function getPathArray(polygon) {
return polygon.getPath().getArray().map(p => {
return { lat: p.lat(), lng: p.lng() }
})
}
然后使用打印到控制台的json并导入它
bermudaTriangle.setPath(JSON.parse(myJson))
- 如何使用传单pip插件与纯GeoJSON数据
- 使用AJAX调用将GeoJSON数据拉入传单
- 从传单数据到GeoJSON的多边形
- 基于geojson数据的传单标记
- 传单弹出窗口内的高图图表使用geojson数据和oneach特征
- 发布到GEOJSON谷歌将数据映射到我的远程服务器上的json文件
- 在 Google Maps V3 中获取 GeoJSON 数据层的属性
- 如何访问放置在谷歌地图上的geoJSON数据中的数据对象
- 将geojson数据与AngularJS可扩展UI-grid一起使用
- 使用实时数据对GeoJSON文件中的许多标记进行样式化
- 在GeoJson数据接收到的谷歌地图多边形上贴上标签
- 转换Geojson嵌套数组数据并在jquery中将其打印为列表
- 如何在Java中将PostGIS数据库中的数据转换为GeoJSON
- HIghcharts地图从GeoJSON数据显示太小
- 从Google Maps导出geoJSON数据
- 传单-显示GeoJSON数据只有当某些层被选中
- 如何在ArcGIS Javascript地图中导入GeoJSON数据
- 字符串str总是空的,尽管它填充了.geojson文件中的数据
- 无法在Cordova的SQLite中保存JSON (GeoJSON)数据
- 在传单中添加传单标记到geojson数据