Tag Archives: longitude

Get latitude and longitude from address PHP

<?php
$country = “IN”;
$state = “Kerala”;
$city = “Kochi”;
$zipcode = “682030”;
$address = “PHPCodez”;

$query_string = “$country+$state+$city+$zipcode+$address”;
$address_encode = urlencode($query_string);
$geocode = ‘http://maps.google.com/maps/geo?q=’.$address_encode.’&output=json&sensor=false’;
$data = file_get_contents($geocode);
$output= json_decode($data);

if($output->Status->code == ‘620’){
sleep(2);
$geocode = ‘http://maps.google.com/maps/geo?q=’.$address_encode.’&output=json&sensor=false’;
$data = file_get_contents($geocode);
$output= json_decode($data);
} elseif ($output->Status->code == ‘602’){
$query_string = “$country+$state”;
$address_encode = urlencode($query_string);
$geocode = ‘http://maps.google.com/maps/geo?q=’.$address_encode.’&output=json&sensor=false’;
$data = file_get_contents($geocode);
$output= json_decode($data);
$longitude_long = $output->Placemark[0]->Point->coordinates[0];
$latitude_lat = $output->Placemark[0]->Point->coordinates[1];
if(($longitude_long == ”) && ($latitude_lat == ”)) {
$query_string = “$country”;
$address_encode = urlencode($query_string);
$geocode = ‘http://maps.google.com/maps/geo?q=’.$address_encode.’&output=json&sensor=false’;
$data = file_get_contents($geocode);
$output= json_decode($data);
}
}

echo $longitude = isset($output->Placemark[0]->Point->coordinates[0])?$output->Placemark[0]->Point->coordinates[0]:”;
echo $latitude = isset($output->Placemark[0]->Point->coordinates[1])?$output->Placemark[0]->Point->coordinates[1]:”;
?>