在Mysql中将坐标保存为多边形类型

时间:2018-01-18 10:46:10

标签: javascript mysql

我在JSON对象中有多边形的坐标,我想将它们保存为数据库中的多边形类型。这是我的代码 -

$Coordinates   = $conn->real_escape_string($_POST['Coordinates']);

"INSERT into table_name (polygon) VALUES(PolyFromText('POLYGON('" .($Coordinates). "')'))";

其中$坐标是多边形顶点的字符串Lat和Long。

对于ex.- Coordinates =

" [36.37485644939407,-96.68243408203125],[35.94465937365276,-96.998291015625],[36.117908916563685,-96.10015869140625]]"

这些是从这段代码生成的 -

google.maps.event.addListener(drawingManager, 'overlaycomplete', function(polygon) {
var coordinates_poly = polygon.overlay.getPath().getArray();

    var lat_poly = [];
    var lng_poly = [];
    var Coordinates = [];
    for(var i = 0; i <coordinates_poly.length; i++){
    lat_poly = coordinates_poly[i].lat();
    lng_poly = coordinates_poly[i].lng();
    Data = [lat_poly,lng_poly];
    Coordinates.push(Data);
    }
    var JSON_Coordinates = JSON.stringify(Coordinates);
    document.getElementById("coordinates").value= '"'+JSON_Coordinates+'"';

但错误是 -

您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以便在&#39; \&#34; [[36.37485644939407,-96.68243408203125],[35.94465937365276,-96.998291015625],[&#39;在第1行

请有人帮我解决此问题。

感谢。

1 个答案:

答案 0 :(得分:0)

使用正确的语法。例如:

var system1 = {
    "System1": [
        {
            "name": "MF3",
            "descr": "Multifilo MF3",
            "speed": [1,2,3,4],
            "time": ["10.01", "10.02", "10.03", "10.04"]
        }
    ]
}
var speed = system1.System1[0].speed
var time = system1.System1[0].time
console.log('Array of Speed', speed)
console.log('Array of Time', time)

//Merge or concatenate two Arrays
var newArray = [...speed, ...time]
console.log('Merged or concatenated Arrays', newArray)

请参阅此处https://dev.mysql.com/doc/refman/5.7/en/gis-polygon-property-functions.html