[SOLVED] Get CompanyDirectory search output sort by distance

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
jack4ya
Power Poster
Power Poster
Posts: 294
Joined: Thu Oct 19, 2006 10:07 am

[SOLVED] Get CompanyDirectory search output sort by distance

Post by jack4ya »

As the TT says.

I got up and running:
CompanyDirectory 1.7.3
CGGoogleMaps 2.1.1
Postcode 1.1.1

With a working search on postal code (geocode).
Now, I can order by a couple of fields (name, postal code, id whatever)

But I needed it sort by distance to search/user inputted post code (aka as geocode.)

Now how do I achieve that?

Now I have a list of results that comply by selected radius. But I can't sort by distance. But the needed information seems there?
Last edited by jack4ya on Fri Mar 04, 2011 8:45 am, edited 1 time in total.
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: Get CompanyDirectory search output sorted by distance

Post by jmcgin51 »

jack4ya wrote:As the TT says.

I got up and running:
CompanyDirectory 1.7.3
CGGoogleMaps 2.1.1
Postcode 1.1.1

With a working search on postal code (geocode).
Now, I can order by a couple of fields (name, postal code, id whatever)

But I needed it sort by distance to search/user inputted post code (aka as geocode.)

Now how do I achieve that?

Now I have a list of results that comply by selected radius. But I can't sort by distance. But the needed information seems there?
Haven't done this, so this is speculation... Along with your list of companies within X radius, do you know the distance to each company? If you have this information available to you, you should be able to customize the template to display them in asc or desc order.

I could be completely off base - it wouldn't be the first time.
jack4ya
Power Poster
Power Poster
Posts: 294
Joined: Thu Oct 19, 2006 10:07 am

Re: Get CompanyDirectory search output sorted by distance

Post by jack4ya »

No, it's not known beforehand, it's *should* be calculated depending on user iput:

An user puts in a postcal code. This gets translated to geocode.
This is compared (within the bounding box set by user inputted radius) to the database with companies (and their postalcode/geocode).
Companies within that boundingbox/radius are then displayed.

Now for each result I need the distance from "inputted postal code" to "postal code from company".

Then I need to be able to sort them by distance...

Which to me seems to be the only usuable way.

What point is it to have a list 'closest to point x' and the display them at random.
jack4ya
Power Poster
Power Poster
Posts: 294
Joined: Thu Oct 19, 2006 10:07 am

Re: Get CompanyDirectory search output sorted by distance

Post by jack4ya »

Okay... I have found this

http://andrew.hedges.name/experiments/haversine/

Code: Select all

	<__script__ type="text/javascript">
	/*
	
	This script is pretty basic, but if you use it, please let me know.  Thanks!
	Andrew Hedges, andrew(at)hedges(dot)name
	
	*/
	
	var Rm = 3961; // mean radius of the earth (miles) at 39 degrees from the equator
	var Rk = 6373; // mean radius of the earth (km) at 39 degrees from the equator
		
	/* main function */
	function findDistance(frm) {
		var t1, n1, t2, n2, lat1, lon1, lat2, lon2, dlat, dlon, a, c, dm, dk, mi, km;
		
		// get values for lat1, lon1, lat2, and lon2
		t1 = frm.lat1.value;
		n1 = frm.lon1.value;
		t2 = frm.lat2.value;
		n2 = frm.lon2.value;
		
		// convert coordinates to radians
		lat1 = deg2rad(t1);
		lon1 = deg2rad(n1);
		lat2 = deg2rad(t2);
		lon2 = deg2rad(n2);
		
		// find the differences between the coordinates
		dlat = lat2 - lat1;
		dlon = lon2 - lon1;
		
		// here's the heavy lifting
		a  = Math.pow(Math.sin(dlat/2),2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon/2),2);
		c  = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a)); // great circle distance in radians
		dm = c * Rm; // great circle distance in miles
		dk = c * Rk; // great circle distance in km
		
		// round the results down to the nearest 1/1000
		mi = round(dm);
		km = round(dk);
		
		// display the result
		frm.mi.value = mi;
		frm.km.value = km;
	}
	
	
	// convert degrees to radians
	function deg2rad(deg) {
		rad = deg * Math.PI/180; // radians = degrees * pi/180
		return rad;
	}
	
	
	// round to the nearest 1/1000
	function round(x) {
		return Math.round( x * 1000) / 1000;
	}
	
	</__script>
Nice... Now if I only could stick it to the results...
jack4ya
Power Poster
Power Poster
Posts: 294
Joined: Thu Oct 19, 2006 10:07 am

Re: Get CompanyDirectory search output sorted by distance

Post by jack4ya »

Okay, apperently in 1.8 it *should* be possible to have this work.

However there is NO documentation on how to achieve.
Or what code/template to use.

I really *really* could use some help here...

The search in 1.8 seems to be failing. Sort of. IF it outputs any results, it's only 2 results, and the don't make sense...
- The are farther in distance than radius should allow.
- Other results should be there, but are not.
fireboydesign
Forum Members
Forum Members
Posts: 17
Joined: Sun Sep 20, 2009 10:42 pm
Location: Portsmouth, UK

Re: [SOLVED] Get CompanyDirectory search output sort by dist

Post by fireboydesign »

Hi, I'm looking for a solution to the same problem you are having. I noticed you have marked this topic as solved, so could you explain your solution to me? Thanks!
jack4ya
Power Poster
Power Poster
Posts: 294
Joined: Thu Oct 19, 2006 10:07 am

Re: [SOLVED] Get CompanyDirectory search output sort by dist

Post by jack4ya »

There were some typos in the code.

More info here:

http://dev.cmsmadesimple.org/bug/view/6198
Post Reply

Return to “Modules/Add-Ons”