http://maps.google.com/maps/api/geocode/json?address=Kunkala&sensor=false
<?php
class Geo extends CI_Controller{
/*
Country, State & City from Google's Geocoding API
*/
public function _remap($address){
$this->index($address);
}
public function index($address){
/* Usage: In my case, I needed to return the State and Country of an address */
$myLocation = $this->reverse_geocode($address);
echo $myLocation;
}
function reverse_geocode($address) {
$address = str_replace(" ", "+", "$address");
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
$result = file_get_contents("$url");
$json = json_decode($result);
foreach ($json->results as $result) {
foreach($result->address_components as $addressPart) {
if((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types)))
$city = $addressPart->long_name;
else if((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types)))
$state = $addressPart->long_name;
else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types)))
$country = $addressPart->long_name;
}
}
if(($city != '') && ($state != '') && ($country != ''))
$address = $city.', '.$state.', '.$country;
else if(($city != '') && ($state != ''))
$address = $city.', '.$state;
else if(($state != '') && ($country != ''))
$address = $state.', '.$country;
else if($country != '')
$address = $country;
// return $address;
return "$country/$state/$city";
}
}
?>
<?php
class Geo extends CI_Controller{
/*
Country, State & City from Google's Geocoding API
*/
public function _remap($address){
$this->index($address);
}
public function index($address){
/* Usage: In my case, I needed to return the State and Country of an address */
$myLocation = $this->reverse_geocode($address);
echo $myLocation;
}
function reverse_geocode($address) {
$address = str_replace(" ", "+", "$address");
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
$result = file_get_contents("$url");
$json = json_decode($result);
foreach ($json->results as $result) {
foreach($result->address_components as $addressPart) {
if((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types)))
$city = $addressPart->long_name;
else if((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types)))
$state = $addressPart->long_name;
else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types)))
$country = $addressPart->long_name;
}
}
if(($city != '') && ($state != '') && ($country != ''))
$address = $city.', '.$state.', '.$country;
else if(($city != '') && ($state != ''))
$address = $city.', '.$state;
else if(($state != '') && ($country != ''))
$address = $state.', '.$country;
else if($country != '')
$address = $country;
// return $address;
return "$country/$state/$city";
}
}
?>
0 comments:
Post a Comment