I can now get the latitude and longitude of my visitors using a mobile browser on my website. I have a database of over 1000 locations on my website as posts in a custom wordpress content type. Each location has a latitude and longitude associated with it.
I’d like the site to work like this:
When a user arrives, it asks them to share their location (DONE)
It then stores their lat and long in PHP variables (EASY, NEXT)
Now take their lat and long, and query the database, and list the closest posts like this:
—–Post title 1 (1.3 miles away)
—–Post title 2 (1.7 miles away)
etc..
How can I use PHP to do this in my wordpress installation?
Sorry to edit this so soon. I found a page from Google and will see if it can help with what I need. https://developers.google.com/maps/articles/phpsqlsearch_v3?hl=hu-HU
Major problem here, my lat and long are stored like this in the database:
Table: wp_postmeta
Column1: meta_key (This contains many different values, but all I care about are the ones containing ‘location_latitude’ or ‘location_longitude’)
Column2: 46.346940 (Contains lat or long numbers depending on what’s in Column1)
So how can I convert this query to work with my database?
SELECT post_id, ( 3959 * acos( cos( radians(37) ) * cos( radians( LATITUDE ) ) * cos( radians( LONGITUDE ) - radians(-122) ) + sin( radians(37) ) * sin( radians( LATITUDE ) ) ) ) AS distance FROM wp_postmeta HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;
Try this post. Basically you’ll need to calculate the distance between two lat-long pairs. It’ll be some spherical math, maybe using the Haversine distance formula.
Additionally, MySQL does support spatial queries, if you’re afraid of math.