我想知道如何給(並檢索)一個ID到小冊子(.draw)中的多邊形。我需要這個,因爲我想能夠告訴數據庫哪個多邊形要刪除/編輯。識別要刪除的多邊形
在此先感謝
編輯:
在我的數據庫我節省了polygon_ID和多邊形的座標。這是我救一個多邊形的代碼:(當我完成繪製多邊形這trigered)
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 \t \t = $_GET['t'];
$action \t \t = $_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) {
\t
echo "L.polygon(" . $row['coordinates'] . ")
\t .addTo(drawnItems);
\t //console.log(pol.options.id);
";
}
我真的希望這個澄清的事情。
取決於您存儲多邊形的格式,我們無法從您的非常簡短的問題中解決問題。請閱讀此:http://stackoverflow.com/help/how-to-ask – iH8
我很抱歉不夠清楚。在我的數據庫中,我存儲了多邊形手柄的所有經度和緯度位置,並且每個多邊形都有ID。我的問題是如何將傳單和mysql ID鏈接在一起,以便我可以告訴數據庫要刪除的多邊形。我使用AJAX來執行查詢。也許你現在可以幫助我? –
仍然取決於如何初始化多邊形以及將它們添加到地圖的方式。您能否編輯您的問題並在您的評論中添加您輸入的信息,並在您如何將多邊形添加到地圖中時包含相關的代碼?單獨的多邊形? FeatureLayer? GeoJSON的?太多的變量給出了很好的答案 – iH8