If you would like to get address from latitude and longitude. You can use below function:
public function getAddressbylatlong($latitude, $longitude){
$latlng = $latitude. ','.$longitude;
$geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?latlng='.$latlng.'&sensor=true');
$geocodedinfo = json_decode($geocode);
foreach ($geocodedinfo->results[0]->address_components as $component) {
$convertedComponent = (array) $component;
#country
if (in_array('country', $convertedComponent['types'])) {
$this->country = $convertedComponent['long_name'];
}
#postcode
if (in_array('postal_code', $convertedComponent['types'])) {
$this->postcode = $convertedComponent['long_name'];
}
#city
if (in_array('administrative_area_level_2', $convertedComponent['types'])) {
$this->city = $convertedComponent['long_name'];
}
#state
if (in_array('administrative_area_level_1', $convertedComponent['types'])) {
$this->state = $convertedComponent['long_name'];
}
#district
if (in_array('administrative_area_level_2', $convertedComponent['types'])) {
$this->district = $convertedComponent['long_name'];
}
#st_addr1
if (in_array('locality', $convertedComponent['types'])) {
$this->st_addr1 = $convertedComponent['long_name'];
}
#st_addr2
if (in_array('route', $convertedComponent['types'])) {
$this->st_addr2 = $convertedComponent['long_name'];
}
}
$address = $this->st_addr1.' '.$this->st_addr2.' '.$this->city.' '.$this->state.' '.$this->postcode.' '.$this->country;
return $address;
}
0 Comment(s)