0

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

}

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

4

1 回答 1

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参数i并从数据库中删除多边形:

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 以来已经有很长时间了。但据我所知,应该没问题。祝你好运!

于 2016-02-15T13:30:17.497 回答