Get all records from MySQL database that are within Google Maps .getBounds?

前端 未结 10 1023
既然无缘
既然无缘 2020-12-02 12:53

Ok I have a database with about 1800 rows, each has a column lat and long, what I am trying to do, it query against Google Maps V3 .getBounds

相关标签:
10条回答
  • 2020-12-02 13:19

    I haven't used Google's API, but my understanding is that if you get ((33.564398518424134, -79.38014701875002), (35.375726155241175, -76.08424858125002)) back from a getBounds call then (33.564398518424134, -79.38014701875002) is the Southwest corner and (35.375726155241175, -76.08424858125002) is the Northeast. I say that as I think they return the Northeast and Southwest corners and I'm assuming the points are latitude, longitude.

    If that's correct, then Bensiu's query would work. Typically using BETWEEN is more efficient.

    SELECT * FROM tilistings WHERE lat BETWEEN a AND c AND lng between b AND  d
    
    0 讨论(0)
  • 2020-12-02 13:19

    From my Gist https://gist.github.com/jesuGMZ/0d7f38d80e2f67d0bc4b7fb620345344

    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:

    • https://dev.mysql.com/doc/refman/5.7/en/gis-general-property-functions.html
    • https://dev.mysql.com/doc/refman/5.7/en/spatial-relation-functions-mbr.html
    • https://dev.mysql.com/doc/refman/5.7/en/spatial-convenience-functions.html#function_st-makeenvelope
    • https://dev.mysql.com/doc/refman/5.7/en/creating-spatial-indexes.html
    0 讨论(0)
  • 2020-12-02 13:24

    I now it's to late for the comment, but maybe it will be usefull for someone. sergio unswer is not quite correct the query for whole world has to look a little bit different, if I'm not mistaken something like this one:

    SELECT * FROM tilistings WHERE (sw_lat < ne_lat AND lat BETWEEN sw_lat AND ne_lat) OR (sw_lat > ne_lat AND (lat BETWEEN sw_lat AND 180 OR lat BETWEEN -180 AND ne_lat)) (sw_lon < ne_lon AND lon BETWEEN sw_lon AND ne_lon) OR (sw_lon > ne_lon AND (lon BETWEEN sw_lon AND 180 OR lon BETWEEN -180 AND ne_lon))

    0 讨论(0)
  • 2020-12-02 13:26

    A slightly simplified version of Vladimir's answer is working perfectly for me.

    Taking an area bound by a box with a southern edge (south), a western edge (west), a northern edge (north) and an eastern edge (east). These can be derived from the Google Map .getBounds which provide south-west corner and north-east corner - you only need the two corners as these fully describe the square bounding box.

    In this example our database table is called locations and contains a column for latitude and longitude.

    SELECT * FROM locations WHERE
    (latitude BETWEEN south AND north) AND
    ((west < east AND longitude BETWEEN west AND east) OR
     (west > east AND (longitude BETWEEN west AND 180 OR longitude BETWEEN -180 AND east)))
    

    This works based on the fact that we only need to account for crossing 180/-180 longitude line as a special case - i.e. where the western longitude is a higher value than the eastern longitude. In any case where we do not cross that line the western longitude will always be less than the eastern longitude.

    With regards to latitude, the southern edge will always be a lower value than the northern latitude as there is no concept of wrapping around over the poles. To cover an area over the north pole, for example we simply have all longitudes (i.e. from -180 to 180) and latitude from the southern boundary to +90.

    0 讨论(0)
提交回复
热议问题