New to the site Have a question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sprks79
Forum Newbie
Posts: 2
Joined: Fri Jan 09, 2009 12:22 pm

New to the site Have a question

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: New to the site Have a question

Post by pickle »

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.
sprks79
Forum Newbie
Posts: 2
Joined: Fri Jan 09, 2009 12:22 pm

Re: New to the site Have a question

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: New to the site Have a question

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: New to the site Have a question

Post 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.
Post Reply