How to get formatted address by Google Maps API

Standard

This is an example procedural steps for getting level wised (City, State/Division, Country, Latitude, Longitude) geolocation from a given address string.

This example can be implemented on a input form where user will put his physical address and your system will store the additional information for further use like for generating location based query corresponding to that address. For example your system can suggest him near by places using his latitude, longitude by calling another google maps API.

<?php
    $address = urlencode('Bogra,Bangladesh');

    $url = "http://maps.google.com/maps/api/geocode/json?address=".$address."&sensor=false&language=en";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_GET, true);
    $response = curl_exec($ch);
    curl_close($ch);
    
    if( $response ) {
        $res    = json_decode( $response, true );        
        
        $locationDetail = array();
        
        if( (isset ($res['status'])) && ($res['status'] == 'OK') ) {
            
            /* 
             * Format address whether it has more than one admin level
             */
            $address_components = $res['results'][0]['address_components'];
            $geometry = $res['results'][0]['geometry'];
            $address = array();
            
            foreach ($address_components as $components):
                    $address[$components['types'][0]] = array ('short_name' => $components['short_name'], 'long_name' =>  $components['long_name']);
            endforeach;  
            
            $address['lat'] = (String) $geometry['location']['lat'];
            $address['lng'] = (String) $geometry['location']['lng'];            
            
            $locationDetail['city'] = (($address['locality']['long_name'] != 'false') && ($address['locality']['long_name'] != '')) ? $address['locality']['long_name'] : 'Not Found';
            $locationDetail['state'] = (($address['administrative_area_level_1']['short_name'] != 'false') && ($address['administrative_area_level_1']['short_name'] != '')) ? $address['administrative_area_level_1']['short_name'] : 'Not Found';
            $locationDetail['state_name'] = (($address['administrative_area_level_1']['long_name'] != 'false') && ($address['administrative_area_level_1']['long_name'] != '')) ? $address['administrative_area_level_1']['long_name'] : 'Not Found';
            $locationDetail['country'] = (($address['country']['short_name'] != 'false') && ($address['country']['short_name'] != '')) ? $address['country']['short_name'] : 'Not Found';
            $locationDetail['country_name'] = (($address['country']['long_name'] != 'false') && ($address['country']['long_name'] != '')) ? $address['country']['long_name'] : 'Not Found';
            
            $locationDetail['lat'] = (($address['lat'] != 'false') && ($address['lat'] != '')) ? $address['lat'] : 'Not Found';
            $locationDetail['lon'] = (($address['lng'] != 'false') && ($address['lng'] != '')) ? $address['lng'] : 'Not Found';
        }
    }

    var_dump($locationDetail);

The output should be:

array(7) {
  ["city"]=>
  string(5) "Bogra"
  ["state"]=>
  string(8) "Rajshahi"
  ["state_name"]=>
  string(8) "Rajshahi"
  ["country"]=>
  string(2) "BD"
  ["country_name"]=>
  string(10) "Bangladesh"
  ["lat"]=>
  string(5) "24.85"
  ["lon"]=>
  string(10) "89.3666667"
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s