Jump to content

Sorting data by smallest distance first from Distance Matrix service


speckytwat

Recommended Posts

My apologies once again. I certainly wasn't trying to imply superhuman capabilities. The output is as follows (obviously I can't output people's actual names, emails and postcodes)

 

I inputted postcode AB14 0TQ

 

And got:

 

 

Name...
Email...
 
Postcode...
 
Distance from you: 36.359845428702
 
Name...
Email...
 
Postcode...
 
Distance from you: 36.967608439871
 
Name...
Email...
 
Postcode...
 
Distance from you: 36.975387110339
 
Name...
Email...
 
Postcode...
 
Distance from you: 37.157217528884
 
Name...
Email...
 
Postcode...
 
Distance from you: 37.190266800979
 
Name...
Email...
 
Postcode...
 
Distance from you: 37.265837397902
 
Name...
Email...
 
Postcode...
 
Distance from you: 37.318324090488
 
Name...
Email...
 
Postcode...
 
Distance from you: 3265.733830295
 
Name...
Email...
 
Postcode...
 
Distance from you: 3265.733830295
 
Name...
Email...
 
Postcode...
 
Distance from you: 3265.733830295
Link to comment
Share on other sites

I've managed to get better output by altering the query as follows: (changed the WHERE m.postcode)

 

 

$sql = "SELECT FirstName, Surname, Email, Postcode, POW(m.Latitude - p.Latitude, 2) + POW(m.Longitude - p.Longitude, 2) AS distance FROM members as m CROSS JOIN (SELECT latitude, longitude FROM postcodelatlng WHERE postcode = ? LIMIT 1) as p WHERE m.postcode NOT LIKE '' ORDER BY distance ASC";

 

So it now shows all the results, in the right order- there are a few incomplete postcodes for a few of the members which I've disregarded.

 

Only odd thing is that the distance is now showing as very small- nearest result when I searched using a postcode roughly 10 to 20km from most of the members was 0.010025634852, then 0.014432975702997 then 0.016619182127393 and so on.

Link to comment
Share on other sites

All that was required was their postcodes and the coordinates you entered for them from your members tables. That data would totally anonyymous. Plus you could have PMed it to me to avoid making anything public.

 

If you have a number of members all with the same wrong distance then it sounds like they all have the same, wrong coordinates - probably 0,0 as the distances are large.

 

Good luck, but you are not going to get a working query if your data is wrong.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.