html - 传单绘制 - toGeoJSON
问题描述
我还是 Leaflet 的新手,并且一直在自学。我使用 Leaflet Draw 库制作了一张效果很好的地图,我想添加一个函数,允许用户将绘制的特征保存到 GeoJSON 文件中。我已经尝试使用本指南,但我仍然没有任何运气。
我正在使用导出功能的按钮:
<input type='button' id='export' value='Export Feature (.kml)' class='btn' />
具有以下主体:
var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
osmAttrib = '© <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> ',
osm = L.tileLayer(osmUrl, { maxZoom: 18, attribution: osmAttrib }),
map = new L.Map('map', { center: new L.LatLng(-29.5, 24.85), zoom: 6 }),
drawnItems = L.featureGroup().addTo(map);
L.control.layers({
'Street': osm.addTo(map),
"Satellite": L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
attribution: 'Tiles © Esri</a>'
})
}, {'Survey Area': drawnItems }, { position: 'topleft', collapsed: false }).addTo(map);
map.addControl(new L.Control.Draw({
edit: {
featureGroup: drawnItems,
poly: {
allowIntersection: false
}
},
draw: {
polygon: {
allowIntersection: false,
showArea: true
}
}
}));
map.on(L.Draw.Event.CREATED, function (event) {
var layer = event.layer;
drawnItems.addLayer(layer);
});
document.getElementById('export').onclick = function(e) {
// Extract GeoJson from featureGroup
var data = featureGroup.toGeoJSON();
// Stringify the GeoJson
var convertedData = 'text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(data));
// Create export
document.getElementById('export').setAttribute('href', 'data:' + convertedData);
document.getElementById('export').setAttribute('download','data.geojson');
}
我认为问题出在 featureGroup 或 drawItems 上,但我就是想不通。
解决方案
推荐阅读
- uwp - 如何让 UWP ContentDialog 的 SecondaryButton 设置焦点?
- java - 用java解析网站时如何使用apikey?
- r - 数据表不同子集之间的 Kruskal-Wallis 秩检验
- python - 我不断收到缩进错误,我不应该
- c# - 有没有办法让两个子对象使用继承引用同一个基础对象?
- java - 错误 500:javax.servlet.ServletException:过滤器 [springSecurityFilterChain]:无法初始化
- c - main的多重定义;只有一个主要项目
- php - 类型:ParseError 消息:语法错误,意外的 'while' (T_WHILE) -- Codeiginiter
- excel - 如何使 VBA Outlook 对象库兼容不同的 Excel 版本?
- symfony - StofDoctrineExtensionsBundle SoftDeleteable