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
New to the site Have a question
Moderator: General Moderators
Re: New to the site Have a question
What are you using to determine the position & therefore the distance? Or is "distance" a stored database field?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: New to the site Have a question
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
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- jaoudestudios
- DevNet Resident
- Posts: 1483
- Joined: Wed Jun 18, 2008 8:32 am
- Location: Surrey
Re: New to the site Have a question
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...
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.
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 />";
}
}...