How to get Latitude, Longitude and Postal Code From Address using PHP and Google geo api

get-zipcode-address-php

In this post I am going to talk about How to get Latitude, Longitude and Postal Code From Address using PHP and Google geo api, Suppose you have list of locations / addresses in your database but you don’t have their latitude, longitude and pincode, then you can use following functions to get their lat, lng and postal code easily, You should thanks to google because their google geo api solves lot’s of address and maps related query in less effort. I have created two functions with exceptional handling which quickly return lat, lng and zipcode.
get-zipcode-address-php

PHP Function to get Latitude, Longitude From Address using Google Geo Api

function extractLatLngFromAdd($address) {
 try {
if(!isset($address) || empty($address)) {
   throw new exception("Address is not set.");
 }
 
$geodata =   json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false')); 
if(!$geodata) {
  throw new exception("Error to extract geodata.");
}
// extract latitude and longitude
$result['latitude']  = $geodata->results[0]->geometry->location->lat; 
$result['longitude'] = $geodata->results[0]->geometry->location->lng;
if(empty($result)) {
 throw new exception("Couldn't extract latitude and longitude, Plz try to input different address.");
}
return $result;
} catch (Exception $e) {
   return $e->getMessage();
}
}
 
print_r(extractLatLngFromAdd("barra kanpur"));

Pass address on above function and it’ll query to google for geo related data for given address and will return latitude and longitude, I have stored lat, lng in array, you can use print_r to see output.

PHP Function to get Postal Code From Address using Google Geo Api

I added few more lines in above function, after getting lat, lng I queried gain to google geo api with lat lng and api return full address details, Now you can easily extract postal code from the output, see below function.

function extractPostalCodeFromAdd($address){
try {
if(!isset($address) || empty($address)) {
   throw new exception("Address is not set.");
 }
 
$geodata =   json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false')); 
if(!$geodata) {
  throw new exception("Error to extract geodata.");
}
// extract latitude and longitude
$lat  = $geodata->results[0]->geometry->location->lat; 
$lng = $geodata->results[0]->geometry->location->lng;
 
$geodata = json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$lat.','.$lng.'&sensor=false')); 
if(!$geodata) {
  throw new exception("Error to extract geodata.");
}
// extracting postcode from address components
foreach($geodata->results[0]->address_components as $addAttr){
                if($addAttr->types[0] == 'postal_code'){
                    if(empty($addAttr->long_name) || !isset($addAttr->long_name)) {
                       throw new exception("Postal Codenot found, Plz try to input different address.");
                     }
                    return $addAttr->long_name;
 
                } 
            }
 } catch (Exception $e) {
   return $e->getMessage();
}
}
 
echo extractPostalCodeFromAdd("barra kanpur");

Note: I would suggest you to generate google map api key and pass to requested URL, To get uninterrupted results.
Replace above google api Url’s from this https://maps.googleapis.com/maps/api/geocode/json?address=[Your_Address]&sensor=false&key=[API_KEY]

See Live Demo

Leave a Reply

Your email address will not be published. Required fields are marked *

Top