unemployment Posted December 28, 2011 Share Posted December 28, 2011 This is more of a logic question. I have built a beta version of my site and I am currently working on getting beta V2 out, but I want to implement location based searching of users (particularly with a google api integration). Aside from the API stuff, how do I perform location based searches on the backend. For instance, I have all of my users locations (city, state, country), but how can I say to grab all users by that state when that state is typed in or grab all users by that city when the city is typed in and the same thing for country? Is there anyway to set this up with a default radius too? So if I search for a town, I get a 20 Mile radius result? I really have no idea how to go about the dynamic look up match. Any good tutorials on this? Quote Link to comment https://forums.phpfreaks.com/topic/253922-how-to-perform-location-based-searches/ Share on other sites More sharing options...
DWilliams Posted December 28, 2011 Share Posted December 28, 2011 Getting all users by a state or city is very easy. Assuming you have this data in a database then just query with " WHERE city='whatever' " or the same for state/country. Distance based calculations are a bit more complicated, though. First you'll need to get the latitude and longitude for each city. The Google Maps API could probably help you here. They have services where you can pass addresses in and get the latitude/longitude returned. Once you have that you can calculate the distance between two lat/long points with a bit of math. See http://www.movable-type.co.uk/scripts/latlong.html for details on how to do that (implementation is in JavaScript but it shouldn't be too hard to convert to PHP). Assuming you don't have thousands and thousands of users you could probably just loop through each user and calculate their distance from the city in question and kick them out of the result set if they are too far away Quote Link to comment https://forums.phpfreaks.com/topic/253922-how-to-perform-location-based-searches/#findComment-1301747 Share on other sites More sharing options...
unemployment Posted December 28, 2011 Author Share Posted December 28, 2011 I guess I'll start with the location based implementation first and work my way towards a more advanced approach. Quote Link to comment https://forums.phpfreaks.com/topic/253922-how-to-perform-location-based-searches/#findComment-1301757 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.