-
Notifications
You must be signed in to change notification settings - Fork 15
Description
Laravel 4 zipcode at a specific radius code, as a helper for anyone doing this (may not be the finest execution strategy but it works). Uses a CSV zipcode table for the US, can be expanded for international usage:
$zipcode = Input::get('postal');
$radius = Input::get('radius');
...
$address_data = Postcode::lookup($zipcode);
$longitude = $address_data['longitude'];
$latitude = $address_data['latitude'];
/* lat log radius work */
// Latitude calculation
$limit = (1 / 69.1703234283616) * $radius;
$latitude_min = $latitude - $limit;
$latitude_max = $latitude + $limit;
// Longitude calculation
$limit = (1 / (69.1703234283616 * cos($latitude * (pi()/180)))) * $radius;
$longitude_min = $longitude - $limit;
$longitude_max = $longitude + $limit;
$users = User::join('zipcode', 'dh_user_main.postal', '=', 'zipcode.postal')
//->select('dh_user_main.dh_user_id','dh_user_main.username','dh_user_main.postal')
->whereBetween('zipcode.lng', array($longitude_min,$longitude_max))
->whereBetween('zipcode.lat', array($latitude_min,$latitude_max))
->paginate(10);;
I used this CSV (US ONLY)- http://notebook.gaslampmedia.com/download-zip-code-latitude-longitude-city-state-county-csv/
zipcode schema in mysql -
id int(10) unsigned No Primary NULL auto_increment
postal char(10) No None NULL
lat double(11,6) No None NULL
lng double(11,6) No None NULL
city varchar(60) No None NULL
state varchar(3) No None NULL
county varchar(200) No None NULL
created_at timestamp No None 0000-00-00 00:00:00
updated_at timestamp No None 0000-00-00 00:00:00