0

i want the users on my site to be able to search for other users within a certain distance. e.g 'find all users within [5] miles'. I have some code which does this "as the crow flies" but I want to be able to find the actual distance including roads. I know that i'd need a third party tool to do this but i'm not sure what to use. Does google maps api have this function? My code at the moment:

    $results = $members->prepare("SELECT users.fname, users.lname, activate.profession, activate.town, ROUND((3958.75 * ACOS(SIN(? / 57.2958)*SIN(uk_postcodes.latitude / 57.2958)+COS(? / 57.2958)*COS(uk_postcodes.latitude / 57.2958)*COS(uk_postcodes.longitude / 57.2958 - ? / 57.2958))), 1) as distance FROM uk_postcodes JOIN activate on activate.outward = uk_postcodes.postcode and activate.userid != ? JOIN users on users.user_id = activate.userid WHERE (3958.75 * ACOS(SIN(? / 57.2958)*SIN(uk_postcodes.latitude / 57.2958)+COS(? / 57.2958)*COS(uk_postcodes.latitude / 57.2958)*COS(uk_postcodes.longitude / 57.2958 - ? / 57.2958))) <= ?");

    $lat = $_POST['latitude'];
    $long = $_POST['longitude'];

    $results->bind_param('ssssssss', $lat, $lat, $long, $_SESSION['token'], $lat, $lat, $long, $_POST['distance']);
    $results->execute();

Any help is appreciated, thanks.

Andy Lobel
  • 3,356
  • 9
  • 31
  • 40

2 Answers2

0

The Google Maps direction API will return the distance in miles as well as the travel time. I do believe however that Google requires you to show a google map when using the Google Maps API.

Here's a link to the direction API info:

Google Maps Direction API

You will need to find a way to use the javascript information in PHP or just switch to javascript to find the users.

Hidde Stokvis
  • 442
  • 3
  • 10
  • does that not just show you the directions between two points ? – Andy Lobel Mar 16 '12 at 15:53
  • Scroll down on the page I mentioned above until you find the JSON output. Here you can see there is a distance mentioned in miles. You should be able to read this and use it for your purposes. – Hidde Stokvis Mar 16 '12 at 16:00
  • im sure there is a way to do it, but nothing that i can think of. google maps needs a destination which is required, and i dont have one. so i think il just give up lol – Andy Lobel Mar 16 '12 at 16:10
  • You must have the location of the user? So you use that as a destination and find the distance via the Google Maps API, you can than compare these distances. An easier method would be to use the haversine formula to calculate the distance between the two points (http://www.movable-type.co.uk/scripts/latlong.html) but this is obviously an "as the crow flies" solution. – Hidde Stokvis Mar 16 '12 at 16:15
  • sorry, but the only way i can see to do it is to query my database to find all locations that are both in my database, and in the results from the (find every location within a set radius) google maps api query. – Andy Lobel Mar 16 '12 at 16:28
  • unless im missing something i cant see how to do it with what your saying but il make the answer as correct cos i might be wrong xP cheers for the help – Andy Lobel Mar 16 '12 at 16:28
0

You may find the Google Maps API to be a useful resource. Check out the distance-matrix docs: https://developers.google.com/maps/documentation/distancematrix/

Mr Griever
  • 4,014
  • 3
  • 23
  • 41