2016-02-11 99 views
0

我想知道如何給(並檢索)一個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); 
 
"; 
 

 
}

我真的希望這個澄清的事情。

+1

取決於您存儲多邊形的格式,我們無法從您的非常簡短的問題中解決問題。請閱讀此:http://stackoverflow.com/help/how-to-ask – iH8

+0

我很抱歉不夠清楚。在我的數據庫中,我存儲了多邊形手柄的所有經度和緯度位置,並且每個多邊形都有ID。我的問題是如何將傳單和mysql ID鏈接在一起,以便我可以告訴數據庫要刪除的多邊形。我使用AJAX來執行查詢。也許你現在可以幫助我? –

+0

仍然取決於如何初始化多邊形以及將它們添加到地圖的方式。您能否編輯您的問題並在您的評論中添加您輸入的信息,並在您如何將多邊形添加到地圖中時包含相關的代碼?單獨的多邊形? FeatureLayer? GeoJSON的?太多的變量給出了很好的答案 – iH8

回答

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之後,這已經很長時間了。但據我所知,應該沒問題。祝你好運!

+0

謝謝,這工作! –

+0

不用了,謝謝,你總是歡迎,這就是我的意思:)我喜歡警告你,我不知道這是否會成爲你的實際執行情況,它是相當不安全的。你真的需要在服務器端進行一些輸入驗證和驗證,因爲這樣很容易被濫用。只需訪問URL inc/send.php?a = delete&t = polygon&i =(猜一些id)'就可能刪除任何多邊形。惡意用戶將有一個現場日 – iH8

+0

我知道,我把它放在一個CMS(當然有安全登錄),只有某些用戶有權編輯/刪除多邊形/多段線/等。 –