<?php
function get_info_GMap_with_coords($latitude, $longitude)
{
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng={$latitude},{$longitude}&sensor=false";
$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'] . " ";
continue;
}
if (strcmp($arr1['types'][0], "route") == 0) {
$address .= $arr1['long_name'];
continue;
}
if (strcmp($arr1['types'][0], "locality") == 0) {
$city = $arr1['long_name'];
continue;
}
if (strcmp($arr1['types'][0], "administrative_area_level_1") == 0) {
$state = $arr1['long_name'];
continue;
}
if (strcmp($arr1['types'][0], "administrative_area_level_2") == 0) {
$state2 = $arr1['long_name'];
continue;
}
if (strcmp($arr1['types'][0], "postal_code") == 0) {
$zip_code = $arr1['long_name'];
continue;
}
if (strcmp($arr1['types'][0], "country") == 0) {
$country = $arr1['long_name'];
continue;
}
}
if (!(!isset($state) || trim($state) === '')) {
$response = array(
"address" => $address,
"city" => $city,
"state" => $state,
"zipcode" => $zip_code,
"country" => $country
); //level_1 administrative data exist
} else {
$response = array(
"address" => $address,
"city" => $city,
"state" => $state2,
"zipcode" => $zip_code,
"country" => $country
); //level_1 administrative data not exist
}
return $response;
}
$lati = '41.900002';
$longi = '12.4833';
print 'Lati: '.$lati;
print '<br />';
print 'Longi: '.$longi;
print '<br />';
print '<br />';
Print 'Address:<br />';
$array = get_info_GMap_with_coords($lati, $longi);
print_r($array);
echo $_SERVER["SERVER_ADDR"];
?>