【问题标题】:How to save a polygon to the database如何将多边形保存到数据库
【发布时间】:2018-06-08 21:46:00
【问题描述】:

我有一个 OpenLayers Web 应用程序。我有一个在绘制多边形后生成 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;

});

请逐步帮助

【问题讨论】:

    标签: php jquery ajax phpmyadmin openlayers


    【解决方案1】:

    保存数据:

    //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 
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 2012-08-25
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 2020-05-06
      • 2017-10-11
      • 1970-01-01
      相关资源
      最近更新 更多