Skip to content

Instantly share code, notes, and snippets.

@jesugmz
Last active July 14, 2021 02:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesugmz/0d7f38d80e2f67d0bc4b7fb620345344 to your computer and use it in GitHub Desktop.
Save jesugmz/0d7f38d80e2f67d0bc4b7fb620345344 to your computer and use it in GitHub Desktop.
Filter items that belongs to a Google Maps boundary with MySQL 5.7

Filter items that belongs to a Google Maps boundary with MySQL 5.7

Having MySQL >5.7 with a table that contains a column type POINT named location and the following Google Maps response:

"geometry": {
    "bounds": {
        "northeast": {
            "lat": 40.5638447,
            "lng": -3.5249115
        },
        "southwest": {
            "lat": 40.3120639,
            "lng": -3.8341618
        }
    },
    //....

you can perform a SQL query to retrieve all your locations contains in that boundary like this:

SELECT * FROM my_table
WHERE Contains(
  ST_MakeEnvelope(
    ST_GeomFromText('POINT(40.5638447 -3.5249115)'),
    ST_GeomFromText('POINT(40.3120639 -3.8341618)')
  ),
  location
);

Consider to index location to improve the performance of your queries if apply. Also, is possible to use Within instead of Contains changing the order of the parameters.

Useful links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment