ashes
ashes

Reputation: 35

Save coordinates as polygon type in Mysql

I have the coordinates of polygon in JSON object and I want to save them as polygon type in database. This is my code-

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

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

where $coordinates are the stringified Lat and Long of the polygon vertices.

For ex.- Coordinates=

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

and these are generating from this code-

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+'"';

but the error is-

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '\"[[36.37485644939407,-96.68243408203125],[35.94465937365276,-96.998291015625],[' at line 1

Please someone help me how to resolve this issue.

Thanks.

Upvotes: 0

Views: 2167

Answers (1)

vadzim dvorak
vadzim dvorak

Reputation: 959

Use correct syntax. For example:

POLYGON((50.866753 5.686455, 50.859819 5.708942, 50.851475 5.722675, 50.841611 5.720615, 50.834023 5.708427, 50.840744 5.689373, 50.858735 5.673923, 50.866753 5.686455))

See here https://dev.mysql.com/doc/refman/5.7/en/gis-polygon-property-functions.html

Upvotes: 1

Related Questions