I'm doing some compare of prices in same location based on GPS coordinates. So eg I've got an item with coordinates:
lat: 45.815005
lng: 15.978501
I want to search in my MySQL database for each items which are eg 500 meters around that place. (it doesn't need to be circle, just 500 meters on X
and 500 meters on Y
both ways). Lat
and lng
are stored as a separate columns type float(10,6)
in my DB
I am aware of the fact it's not easy to calculate exact lng and lat but I'm fine if I miss few meters each site.
This is pretty complex question but I would be thankful for any advise which will kickoff my start.
Calculating the distance between two coordinates isn't actually that difficult given the haversine formula.
SELECT
-- stuff here
, ( 6371000 * acos( cos( radians(45.815005) ) * cos( radians( stuff.lat ) ) * cos( radians( stuff.lng ) - radians(15.978501) ) + sin( radians(45.815005) ) * sin(radians(stuff.lat)) ) ) AS distance
FROM
stuff
HAVING
distance < 500
Necessary changes from the original answer:
The constant offered in the original answer supplied the values for miles or kilometers. I've changed the constant here to work with meters.
The constants have changed to use your coordinates. You might want to adapt the query a little further to make those parameters instead of constants.
The having
expression changed a little to reflect your desire for 500 meters. Again, this might be something you want to parameterize.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.