use*_*511 20 mysql leaflet mapbox
我想使用leaflet.draw来创建区域的轮廓.我设法让这个工作正常:https://www.mapbox.com/mapbox.js/example/v1.0.0/leaflet-draw/
现在我想将每个多边形的数据保存到mysql表中.我有点坚持我将如何导出数据和我应该做的格式.
如果可能的话,我希望将来将数据拉回到地图框/传单地图中,所以猜想像geojson这样的东西会很好.
小智 28
因此,你可以使用draw:created来捕获图层,将其转换为geojson然后将其字符串化以保存在数据库中.我只做了一次这很脏但很有效.
map.on('draw:created', function (e) {
var type = e.layerType;
var layer = e.layer;
var shape = layer.toGeoJSON()
var shape_for_db = JSON.stringify(shape);
});
Run Code Online (Sandbox Code Playgroud)
如果要收集坐标,可以这样做:
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
map.on('draw:created', function (e) {
var type = e.layerType,
layer = e.layer;
drawnItems.addLayer(layer);
var shapes = getShapes(drawnItems);
// Process them any way you want and save to DB
...
});
var getShapes = function(drawnItems) {
var shapes = [];
drawnItems.eachLayer(function(layer) {
// Note: Rectangle extends Polygon. Polygon extends Polyline.
// Therefore, all of them are instances of Polyline
if (layer instanceof L.Polyline) {
shapes.push(layer.getLatLngs())
}
if (layer instanceof L.Circle) {
shapes.push([layer.getLatLng()])
}
if (layer instanceof L.Marker) {
shapes.push([layer.getLatLng()]);
}
});
return shapes;
};
Run Code Online (Sandbox Code Playgroud)