Search code examples
mysqlgoogle-mapsgeolocationmultilinestring

MySQL storing google maps lat/long data


I have created a map which allows the user to plot multiple markers with the intention on storing them in a database table. The original plan was to store multiple rows per one map like so:

------------------------------------
| mapentryid | mapid | Long | Lat  | 
------------------------------------
| 1          | 1     | X.XX | X.XX |
| 2          | 1     | X.XX | X.XX |
| 3          | 1     | X.XX | X.XX |
| 4          | 2     | X.XX | X.XX |
| 5          | 2     | X.XX | X.XX |
| 6          | 2     | X.XX | X.XX |
| 7          | 2     | X.XX | X.XX |
------------------------------------

But I have since found out that you are able to store multilinestring's in MySQL which sounds perfect for what I want (I think?)

I can insert the data correctly using the following SQL query format:

INSERT INTO table (LatLng)
VALUES (
         MultiLineString(
            LineString(Point(x),Point(y)),
            LineString(Point(x),Point(y)),LineString(Point(x),Point(y))
       )

This adds the multilinestring OK, although they actually come up as the following:

phpmyadmin screenshot

The question is, is the above OK? If so, how can I convert this data back into something I am able to display on Google Maps?


Solution

  • Why not use POINT data?

    INSERT INTO geoTable (mapentryid, mapid, coord) VALUES (
        1, 1, GeomFromText('POINT(15 20)')
    );
    

    I always use the 'Well-Known Text (WKT) Format' for geospatial enabled queries in MySQL, they are the most compatible with other systems out there.

    To query the stored values:

    SELECT mapentryid, mapid, X(coord), Y(coord) FROM geoTable;