Below is my code :
$status = 'OK';
$value = '';
try
{
$lat = $_GET ['lat'];
$lon = $_GET ['lon'];
// TODO: validate input + find city name by coord
assert ( is_numeric ( $lat ) );
assert ( is_numeric ( $lon ) );
$google_api_link = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=%s&sensor=false';
$lat = filter_input ( INPUT_GET, 'lat' );
$lon = filter_input ( INPUT_GET, 'lon' );
$link = sprintf ( $google_api_link, $lat . "," . $lon );
$content = httpReq ( $link );
$json_object = json_decode ( $content );
if ($json_object->status != 'OK')
{
$status = $json_object->status;
}
else
{
$address_components = $json_object->results [0]->address_components;
for($i = 0; $i < count ( $address_components ); $i ++)
{
$address_item = $address_components [$i];
if ( in_array('country', $address_item->types) && ( ($i - 1) >= 0 ))
{
$value = $address_components [$i - 1]->long_name;
}
}
}
Thursday, December 6, 2012
Subscribe to:
Post Comments (Atom)