1

SO I have jQuery DataTables setup and running fine. My eventual goal, is to allow the user to use google places autocomplete, to update their location, and then to have a 'sortable' distance column added to my DataTables table, upon refresh.

The plan is is to have a button next to the text input of google autocomplete, that says use this address, once user searches for and finds an address using autocomplete, and then pushes the button , I want to (probably)send them to a loading page for a few seconds whilst we

1)read the text string, format it for use with google geo coding api, aka

"Cranbourne, Victoria, Australia" ----> "Cranbourne+Victoria+Australia"

2)insert it in a geocode request

3)extract the lat and lon values from the resulting json, and assign them to vars

Now comes the hard part, I was thinking using some php, to calculate the distance between the users(lat,long), and every entry that was in the DataTable table, which was was populated with sql data using a foreign key (all sql rows have lat lon data)

Then I was thinking of sending said entries to an xml, only this time every entry will have an extra value "DISTANCE".

Of coarse with some clever code this whole thing wont take a second, the new DataTable table is then populated with the xml, only this time it contains a distance column with distance values for every row, which of coarse will be sortable, without any DataTables hacks what so ever.

So am I stupid, or is this somewhat of a good idea, is there an easier way?

I would really like some answers to this thank you.

2 Answers2

0

That actually could be difficult (depending on your math expertise), because of the fact that it requires using spherical geometry. You might consider using an API:

https://developers.google.com/maps/documentation/distancematrix/

If you want to try doing it yourself, you might want to use a class written for that, such as:

http://www.phpclasses.org/browse/file/36294.html

The second option is probably better, because Google has limits on its API. Once you have a way of calculating distance, it's a simple matter to query the database and create the XML. I don't know why you want to use XML instead of json, but the PHP function json_encode() might make it simpler, and it's supported by DataTables.

Expedito
  • 7,771
  • 5
  • 30
  • 43
0

I know this is a really old question but I just came across it and wanted to recommend using the Haversine Formula to calculate the distances between lat/long values

I've used this implementation and found it worked quite wonderfully

Community
  • 1
  • 1
General_Twyckenham
  • 2,161
  • 2
  • 21
  • 36