Jump to content
Sign in to follow this  
GetPutDelete

Free address lookup API based on postcode?

Recommended Posts

Questions in the title really, has anyone heard of one?

 

There are many free geolocation services out there, One notably maxmind's services, You can filter the database yourself or use said software to get the lookup.

Share this post


Link to post
Share on other sites

// First, we need to take their postcode and get the lat/lng pair:
$postcode = $_REQUEST['postcode'];
// Sanitize their postcode:
$search_code = urlencode($postcode);
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $search_code . '&sensor=false';
$json = json_decode(file_get_contents($url));
$lat = $json->results[0]->geometry->location->lat;
$lng = $json->results[0]->geometry->location->lng;
// Now build the lookup:
$address_url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' . $lat . ',' . $lng . '&sensor=false';
$address_json = json_decode(file_get_contents($address_url));
$address_data = $address_json->results[0]->address_components;
$street = str_replace('Dr', 'Drive', $address_data[1]->long_name);
$town = $address_data[2]->long_name;
$county = $address_data[3]->long_name;
$city = $address_data[4]->long_name;
$country = $address_data[5]->long_name;
$array = array('street' => $street, 'town' => $town, 'city' => $city, 'country' => $country);
//echo json_encode($array);
echo $array = implode(", ",$array);

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.