3 ms·
mySQL 'SELECT zip FROM '.insert table here.' WHERE (POW((69.1(tablelongitude-"'.$obj->longitude.'")cos('.$obj->latitude.'/57.3)),"2")+POW((69.1(tablelatitude-"'
by rainface 18y ago
mySQL 'SELECT zip FROM '.insert table here.' WHERE (POW((69.1(tablelongitude-"'.$obj->longitude.'")cos('.$obj->latitude.'/57.3)),"2")+POW((69.1(tablelatitude-"'.$obj->latitude.'")),"2"))<('.$radius.''.$radius.') ';
You need the origin long and lat, and the radius (distance), but the nice thing is the database does all the radius distance calculations internally without passing it to the scripting language, in a fraction of second depending on the server and DB. This is the query I used in http://mybandsupport.com http://mybandsupport.com. You can see the result by entering the zipcode in the splash screen and clicking search. The function takes your zip, gets the long and lat, then the DB compares it to every zip's (4500) long and lat to find every zip within 20 miles.
Just a note: comparing every zip code and its distance and recording the distance to a DB would create upwards of 12 million records. This might be the breaking point for some.... I was thinking about doing it myself. Once I worked out the numbers I backed away.
If you know the origin and destination long and lat this works well to calc distance.
$dist = sin(deg2rad($this->lat1)) * sin(deg2rad($this->lat2)) + cos(deg2rad($this->lat1)) * cos(deg2rad($this->lat2)) * cos(deg2rad($theta));