如何添加从MyQL中提取的多边形

时间:2017-01-23 22:51:47

标签: leaflet polygon leaflet.draw

我可以绘制一个多边形并将其保存到MySQL数据库中。 (如果有人现在需要,请问)。

我也可以向我的数据库发送SELECT请求并显示已保存的多边形。

以下是我的数据库返回的内容

{
    "type":"FeatureCollection",
    "features":[
        {
            "id":1,
            "type":"Feature",
            "properties":{},
            "geometry":{
                "type":"Polygon",
                "coordinates":[
                    [
                        [6.146185398101807,46.447689601949826],
                        [6.146475076675416,46.44726084421887],
                        [6.1472368240356445,46.44776352535544],
                        [6.1466360092163095,46.447833752497885],
                        [6.146185398101807,46.447689601949826]
                    ]
                ]
            }
        },
        {
            "id":"id",
            "type":"Feature",
            "properties":{},
            "geometry":{
                "type":"Polygon",
                "coordinates":[
                    [
                        [6.146185398101807,46.447689601949826],
                        [6.146475076675416,46.44726084421887],
                        [6.1472368240356445,46.44776352535544],
                        [6.1466360092163095,46.447833752497885],
                        [6.146185398101807,46.447689601949826]
                    ]
                ]
            }
        }
    ]
}

但是如何将带有多边形的图层添加到地图中,可以编辑或删除它,就好像我从绘图控件中绘制一个新的多边形。

因为它不是一个事件,我不能使用它,对吧?:

map.on(L.Draw.Event.CREATED, function (e) {
  var type = e.layerType,
    layer = e.layer;


  FGgpx.addLayer(layer);

  var shape = layer.toGeoJSON()
  var shape_for_db = JSON.stringify(shape);

  console.log("Create");
  console.log(shape_for_db);
  // Save to db

    saveGeofences(1,shape_for_db);
});

我试过没有结果! 但它就像一个创作? ISN'?吨

这是代码,我用来从我的数据库中获取多边形:

function getGeofences(devise_id){
  $.ajax({
    type: "POST",
    url: "maps/sql/getGeofences.php",
    //data: {data:data},
    data: {devise_id:devise_id},
    success: result,
    error: error,
    dataType: "json"
  });

  function error(data)
  {
    console.log("Error getGeofences");
    console.log(data);
  }

  function result(data){
    console.log("Geofences from DB");
    console.log(data);

    // How can I add a layer with the polygons to my map
    //FGgpx.addLayer(data); // This does not works

  }
}

1 个答案:

答案 0 :(得分:0)

var drawnItems = new L.FeatureGroup();

for (var i = 0; i < data.features.length; i++){
  var layer = L.GeoJson.geometryToLayer(data.features[i]);
  layer.addTo(drawnItems);
}

map.addLayer(drawnItems);

for循环遍历GeoJSON中的要素并将它们转换为图层,以便多边形可以显示在地图上。每个要素都添加到drawnItems要素组,该要素组允许将图层分组并作为一个图层处理,因此可以将它们一起添加到地图中。这样,可以将地图的状态设置为绘制多边形之前的状态,然后再将其保存到数据库中。

希望有所帮助, 乔鲍