Monthly Archives: February 2013

PHP get address from coordinate using google api

This function is just a basic function to get basic address from google api, if more detail information needed, you will have to understand the json response string and customize your output from there.

In order to be able to use the google api, you will need to be registered to google developer community and has map api set to on. If you are not registered, you can find go to and start from there.

After you have setup your access to use google api, you are ready to use this function.

function for getting address using coordinates

// This function is for string null or empty check in the main function

function IsNullOrEmptyString($question){

return (!isset($question) || trim($question)==='');


function getAddress($latitude,$longitude){

$url = "$latitude,$longitude&sensor=false&token=".<google api token key here>;

$response = file_get_contents($url);

$json = json_decode($response,TRUE); //set json response to array based

$address_arr = $json['results'][0]['address_components'];

$address = "";

foreach ($address_arr as $arr1){

if(strcmp($arr1['types'][0],"street_number") == 0){

$address .= $arr1['long_name']." ";



if(strcmp($arr1['types'][0],"route") == 0){

$address .= $arr1['long_name'];



if(strcmp($arr1['types'][0],"locality") == 0){

$city = $arr1['long_name'];



if(strcmp($arr1['types'][0],"administrative_area_level_1") == 0){

$state = $arr1['long_name'];



if(strcmp($arr1['types'][0],"administrative_area_level_2") == 0){

$state2 = $arr1['long_name'];



if(strcmp($arr1['types'][0],"postal_code") == 0){

$zip_code = $arr1['long_name'];



if(strcmp($arr1['types'][0],"country") == 0){

$country = $arr1['long_name'];





$response = array("address"=>$address, "city"=>$city, "state"=>$state, "zipcode"=>$zip_code, "country"=>$country); //level_1 administrative data exist


$response = array("address"=>$address, "city"=>$city, "state"=>$state2, "zipcode"=>$zip_code, "country"=>$country); //level_1 administrative data not exist


return $response;


usage of the function. Output is an array.

$array = getAddress(37.7840,-122.4352);

To obtain address simply use:

$array->[‘address’] or
$array->[‘city’] or
$array->[‘state’] or
$array->[‘zipcode’] or

Simple php function to get coordinates from address through google services

Here I am showing you how to get coordinates from address using google api (Thanks to Google). The result will solely depends on how google support it in the future. Because google will set the 1st index result for the most relevant, thus this works quite well in general.

Function in php:

function getCoordinates($address){

$address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern

$url = "$address";

$response = file_get_contents($url);

$json = json_decode($response,TRUE); //generate array object from the response from the web

return ($json['results'][0]['geometry']['location']['lat'].",".$json['results'][0]['geometry']['location']['lng']);


How to call the function in php

echo getCoordinates('740 Story Rd San Jose CA 95122');

Result : 37.328865,-121.8581845