gpt4 book ai didi

javascript - 识别要删除的多边形

转载 作者:行者123 更新时间:2023-11-29 21:29:51 25 4
gpt4 key购买 nike

我想知道如何为传单(.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);
";

}

我真的希望这能澄清事情。

最佳答案

您可以将 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();

});

});

现在在服务器端,捕获 iGET 参数并从数据库中删除多边形:

if(isset($_GET['i']) && isset($_GET['t']) && isset($_GET['a'])){

$id = $_GET['i'];
$type = $_GET['t'];
$action = $_GET['a'];

if ($type == "polygon" && $action == "delete") {

$sth = $dbh->prepare('DELETE FROM polygons WHERE id = :id');
$sth->bindParam(':id', $id);
$sth->execute();

}

}

就是这样。请注意,免责声明:我无法测试它,所以我不得不徒手操作它,而且自从我完成 PHP 以来已经很长时间了。但据我所知,应该没问题。祝你好运!

关于javascript - 识别要删除的多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35347587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com