我想知道如何在传单(.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);
";
}
我真的希望这能澄清事情。