inside lat,lng bounds where query

萝らか妹 提交于 2019-12-23 04:35:26

问题


I am getting lat/lng bounds from google map in client side through xhr to my php script. which is making an sql query to search location within that bounds. My table stores lat, lng as different columns. now how can I arrange my where queries such that only the points within the specified NE, SW bounds are returned.

In database its stored as number and I don't know if there already is any postgresql specific aggregate function to do the same.


回答1:


Am I missing something? It's just a rectangular bounds check. It's not like you need to calculate the distance between the points.

WHERE lat BETWEEN lat_of_corner_a AND lat_of_corner_b
  AND lon BETWEEN lon_of_corner_a AND lon_of_corner_b



回答2:


I posted the working solution here: Get all records from MySQL database that are within Google Maps .getBounds?

Alex's solution only works in USA, specifically in the north hemisphere, west region (were decreasing values fit the order in which Google provides the bouds values.

This one works for the other 3/4 of the world.

SELECT * FROM tilistings WHERE
(CASE WHEN a < c
        THEN lat BETWEEN a AND c
        ELSE lat BETWEEN c AND a
END) 
AND
(CASE WHEN b < d
        THEN lng BETWEEN b AND d
        ELSE lng BETWEEN d AND b
END)

(See the linked post for more details)



来源:https://stackoverflow.com/questions/11214519/inside-lat-lng-bounds-where-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!