Monday, 17 September 2012

using google map lookup address by postal code

       

   <?php
    // First, we need to take their postcode and get the lat/lng pair:
    $postcode = 'SL1 2PH';
    // Sanitize their postcode:
    $search_code = urlencode($postcode);
    $url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $search_code . '&sensor=false';
 //json_decode() returns an object pass 2nd parameter true to json_decode() if you want to get results as array
    $json = json_decode(file_get_contents($url), true);
 //print_r($json);  uncomment for seeing whole arry
     $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';
 //json_decode() returns an object if you want to get results as array, pass 2nd parameter true e.g. json_decode(file_get_contents($address_url, true))
    $address_json = json_decode(file_get_contents($address_url), true);
    print_r($address_json['results'][0]['formatted_address']);
 ?>    

    

0 comments:

Post a Comment