Page 1 of 1
New to the site Have a question
Posted: Fri Jan 09, 2009 12:26 pm
by sprks79
Hey guys and gals, I am fairly new to the site, been lurking around for awhile and finally decided to ask a question. I am working on a site which needs to utilize a search by distance field. I am curious as to if there are any freely available scripts in which to help me accomplish this? It doesn't necessarily have to be php but it would help. Any suggestions would be greatly appreciated.
Thanks for your time
Sprks79
Re: New to the site Have a question
Posted: Fri Jan 09, 2009 3:24 pm
by pickle
What are you using to determine the position & therefore the distance? Or is "distance" a stored database field?
Re: New to the site Have a question
Posted: Sat Jan 10, 2009 1:40 pm
by sprks79
distance would be determined by a stored database field, for example, i as a registering user input my zipcode, it is stored in the db, i am looking for a program to return me a specific output based on a miles difference from set db record.
Re: New to the site Have a question
Posted: Mon Jan 12, 2009 10:59 am
by pickle
The most granular you are getting is a ZIP code? I'm not sure how accurate you can get with such a large area. I'd recommend finding an average latitude & longitude of the ZIP code, then using the Google Map API to determine the latitude & longitude of the address a user enters, then finding a funky formula (do a search, I know it's out there), to determine the distance between the two sets of co-ordinates.
Re: New to the site Have a question
Posted: Mon Jan 12, 2009 11:24 am
by jaoudestudios
I wrote one for the UK postcode a few months back. It was not too hard to do it all in MySQL. The reference table had postcode, longitude and latitude.
Not sure if this will help, but here is my method from my class - might give you some ideas...
Code: Select all
...
// filter postcode and distance
private function filterPostcode($postcode, $distance) {
if (!empty($this->$postcode)) {
$this->getLongLat($this->$postcode);
/*
$this->select .= ", ROUND(SQRT(POWER(postcode.longitude - ".$this->longitude.", 2)
+ POWER(postcode.latitude - ".$this->latitude.", 2)),2) AS distance";
$this->join .= "JOIN postcode ON TRIM(SUBSTR(listing.postcode,1,4)) = postcode.postcode AND ";
$this->join .= " ROUND(SQRT(POWER(postcode.longitude - ".$this->longitude.", 2)
+ POWER(postcode.latitude - ".$this->latitude.", 2)),1) < '".$this->$distance."'<br />";
*/
// change units
if ($this->distanceUnit == 'km') {
$unit = 1.609; // convert to km
}
else {
$unit = 1; // stay with metres
}
// $this->select .= ", SQRT(POW(postcode.x - ".$this->x.",2) + POW(postcode.y - ".$this->y.",2))/1000 AS andyKm"; // compare to andy's results
$this->select .= ", ROUND((3985 * 3.1415926 * SQRT((postcode.latitude - ".$this->latitude.") * (postcode.latitude - ".$this->latitude.") + COS(postcode.latitude/57.29578)*COS(".$this->latitude."/57.29578)*(postcode.longitude - ".$this->longitude.")*(postcode.longitude - ".$this->longitude."))/180)*".$unit.",2) AS distance";
$this->join .= "JOIN postcode ON TRIM(SUBSTR(listing.postcode,1,4)) = postcode.postcode AND ";
$this->join .= " (3985 * 3.1415926 * SQRT((postcode.latitude - ".$this->latitude.") * (postcode.latitude - ".$this->latitude.") + COS(postcode.latitude/57.29578)*COS(".$this->latitude."/57.29578)*(postcode.longitude - ".$this->longitude.")*(postcode.longitude - ".$this->longitude."))/180)*".$unit;
$this->join .= " < '".$this->$distance."'<br />";
}
}...
The rest of the class is specific to the website I wrote it for, you are welcome to have it, but I dont think it will help.