1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?PHP $postcode = $_REQUEST['postcode']; $postcode = "SE12 0UW"; // CHANGE // Get the latitude & longitude of submitted postcode $postcode = urlencode($postcode); $query = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $postcode . '&sensor=false'; $result = json_decode(file_get_contents($query)); $lat = $result->results[0]->geometry->location->lat; $lng = $result->results[0]->geometry->location->lng; // Get the address based on returned lat & long $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; foreach($address_data as $data): $array[$data->types[0]] = $data->long_name; endforeach; echo json_encode($array); ?> |
Returns an array showing {“street_number”:”254A”,”route”:”Baring Road”,”locality”:”London”,”postal_town”:”London”,”administrative_area_level_2″:”Greater London”,”country”:”United Kingdom”,”postal_code”:”SE12 0UW”}