我想知道如何在传单(.draw)中为多边形提供(和检索)ID。我需要这个,因为我希望能够告诉数据库删除/编辑哪个多边形。
提前致谢
编辑:
在我的数据库中,我保存了polygon_ID和多边形的坐标。这是我保存多边形的代码:(当我完成绘制多边形时,这是三角形的)
map.on('draw:created', function(e) {
var type = e.layerType,
layer = e.layer;
if (type == "polygon") {
var polygon = {};
polygon['geometry'] = {};
polygon['geometry']['type'] = "Polygon";
var coordinates = [];
latlngs = layer.getLatLngs();
for (var i = 0; i < latlngs.length; i++) {
coordinates.push([latlngs[i].lat, latlngs[i].lng])
}
polygon['geometry']['coordinates'] = [coordinates];
coordinates = JSON.stringify(coordinates);
//console.log(coordinates);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
//alert("Sent!");
}
};
xhttp.open("POST", "inc/send.php?a=add&t=polygon&c=" + coordinates, true);
xhttp.send();
}
drawnItems.addLayer(layer);
});
这是我的send.php:
if(isset($_GET['c']) && isset($_GET['t']) && isset($_GET['a'])){
$coordinates = $_GET['c'];
$type = $_GET['t'];
$action = $_GET['a'];
if($type == "polygon" && $action == "add"){
$sth = $dbh->prepare('INSERT INTO polygons (coordinates) VALUES (:coordinates)');
$sth->bindParam(':coordinates', $coordinates);
$sth->execute();
}
} else {
}
这是我加载多边形的方式:
$polygonsth = $dbh->prepare("SELECT * FROM polygons");
$polygonsth->execute();
$polygonresult = $polygonsth->fetchAll();
...
foreach ($polygonresult as $row) {
echo "L.polygon(" . $row['coordinates'] . ")
.addTo(drawnItems);
//console.log(pol.options.id);
";
}
我真的希望这能澄清事情。
答案 0 :(得分:0)
您可以将ID作为选项参数传递给多边形实例:
foreach ($polygonresult as $row) {
echo "L.polygon(" . $row['coordinates'] . ", { id: " . $row['id'] . "}).addTo(drawnItems);";
}
现在,当您从drawnItems
图层中删除多边形时,请捕获并处理draw:deleted
事件。它返回一个L.LayerGroup
,你可以迭代它来处理删除的多边形:
map.on('draw:deleted', function (e) {
var layers = e.layers;
layers.eachLayer(function (layer) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
//alert("Deleted!");
}
};
xhttp.open("POST", "inc/send.php?a=delete&t=polygon&i=" + layer.options.id, true);
xhttp.send();
});
});
现在在服务器端,捕获GET
的{{1}}参数并从数据库中删除多边形:
i
那就是它。请注意,免责声明:我无法对其进行测试,因此我必须手写它并且自从我完成PHP以来已经很长时间了。但据我所知,它应该没问题。祝你好运!