Hi! We were seeing about Google Maps API for a while, and today's post is also one on the line. In this tutorial, we are going to see how to get zipcode from address using php and google maps api. Sometimes you might have a large set of customers addresses without postal codes. In such cases you can rely on Google maps to provide the pin code you need. It serves queries related to addresses with less effort. So with the help of Google Maps Geocoding API you can easily extract the zip code from the address and I'm going to show you how to do it in php.
PHP - Get Zipcode from Address using Google Maps API:
Retrieving zipcode/pincode from the address is a two step process. First we have to get the latitude and longitude for the given address and then use them to get the zip code details.
The API offers two different types of services, geocoding and reverse geocoding of addresses and we have to use both to achieve the desired result.
This is the web service url we have to use,
http://maps.google.com/maps/api/geocode/json
With this, you have to attach the appropriate query parameters to the api url. The result will be returned as JSON.
Let's get started!
Step-1) Getting Latitude & Longitude from Address
<?php $geocode = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address"); $json = json_decode($geocode); $latitude = $json->results[0]->geometry->location->lat; $longitude = $json->results[0]->geometry->location->lng; ?>
Step-2) Getting Zipcode using Latitude, Longitude
<?php $geocode = file_get_contents("http://maps.google.com/maps/api/geocode/json?latlng=$latitude,$longitude"); $json = json_decode($geocode); foreach($json->results[0]->address_components as $adr_node) { if($adr_node->types[0] == 'postal_code') { return $adr_node->long_name; } } ?>
The following is the complete php function to retrieve zip code details from the provided address.
PHP Function:
<?php function getZipcode($address) { // get geocode $geocode = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address"); $json = json_decode($geocode); $latitude = $json->results[0]->geometry->location->lat; $longitude = $json->results[0]->geometry->location->lng; // get zipcode $geocode = file_get_contents("http://maps.google.com/maps/api/geocode/json?latlng=$latitude,$longitude"); $json = json_decode($geocode); foreach($json->results[0]->address_components as $adr_node) { if($adr_node->types[0] == 'postal_code') { return $adr_node->long_name; } } return false; } ?>
The function getZipcode()
takes up a single parameter $address
and extracts postal_code
from the provided address using geocode api.
Usage:
You have to call the above function like this in php.
<?php // set address $address = 'Brooklyn, NY, USA'; $address = str_replace(' ', '+', $address); $zipcode = getZipcode($address); if($zipcode) echo 'Zipcode: ' . $zipcode; else echo 'Zipcode not found! Please try different address!!!'; // output // Zipcode: 11216 ?>
What the above function does is, it takes the address info and request Google API for its geocoding details. Receive the response, decodes the json string and extract the latitude and longitude values from it.
Next, request the API once again by passing the extracted geocoding values, but this time for address details with postal code. This step is called reverse geocoding. It again decodes the json response, iterate over to fetch and return the zipcode.
API Key:
Google imposes request limits per day for using the api. Therefore getting the api_key
is the best way for uninterrupted work. Login to your Google account, get the api key and use it in the url like this,
https://maps.googleapis.com/maps/api/geocode/json?address=ADDRESS&key=YOUR_API_KEY http://maps.google.com/maps/api/geocode/json?latlng=$latitude,$longitude&key=API_KEY;Read Also:
That was all about fetching zipcode from address in php and google maps api. Although the process seems handful at first, it is really simple once you get to know it. I recommend you to use the api_key parameter with the url, since you can easily exceed the daily limit. I hope you like this post. Appreciate if you share it in your social circle.
No comments:
Post a Comment