簡體   English   中英

如何將多邊形保存到數據庫

[英]How to save a polygon to the database

我有一個OpenLayers網絡應用程序。 我有一個函數,在繪制多邊形后會生成wkt格式。

function generateWkt() {
    var featureWkt, modifiedWkt;
    var unionFeatures = [];

    layer.getSource().forEachFeature(function(f) {
        var featureClone = f.clone();
        featureWkt = wkt.writeFeature(featureClone);

        if(featureWkt.match(/MULTIPOLYGON/g)) {
            modifiedWkt = (featureWkt.replace(/MULTIPOLYGON/g, '')).slice(1, -1);
        } else {
            modifiedWkt = (featureWkt.replace(/,/g, ', ')).replace(/POLYGON/g, '');
        }

        unionFeatures.push(modifiedWkt);
    });

    layer.getSource().getFeatures().length ? $('#wkt').text('MULTIPOLYGON(' + unionFeatures + ')') : $('#wkt').text('');
}

我想使用一個按鈕將數據wkt(unionFeatures)發送到數據庫(phpmyadmin),然后刷新頁面后,使用第二個按鈕將數據加載到多邊形中並將其顯示在地圖上。

如何修改此代碼以及將什么放在php文件中?

$('#postJson').click(function(){

    $.post('post_receiver.php', { ??? }, function(data){

    $('#response').html(data);

    }).fail(function() {
     alert( "Posting failed." );

    });
    return false;

});

請逐步幫助

要保存數據:

//javascript code
//Send the json object to php file to save to db

$.ajax({
    type: 'post',
    dataType: "json",
    data:JSON.stringify(unionFeatures),
    url: "http://localhost/post_receiver.php", 
    success: function (data) {              
        alert("data sent");                 
    }
});

在post_receiver.php中

$data = json_decode(trim(file_get_contents('php://input')), true);

//Save the $data to db using your logic. You may have to separate 
//multiple geometries WKT and insert separately.            
//***Saving logic ****

要將數據顯示回地圖:

在getData.php中

//Retrieve data as a string through SQL query from db tables.

javascript中的“呼叫”按鈕單擊功能可在地圖上顯示幾何

function showGeometriesOnMap(){
    $.ajax({
        type: 'get',
        dataType: "json",
        url: "http://localhost/getData.php", 
        success: function (data) {              
            var retrievedData = JSON.parse(data);   

            //Display on map logic here 
            //See https://openlayers.org/en/latest/examples/wkt.html to 
            //show on map from WKT 
        }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM