google maps - PHP function for Reverse Geocoding for street number -
the following code from post working me, returning full address (street number, street name, city, state, zip code, country). there way return street number , name instead of full address? google hasn't been help.
<? function getaddress($lat,$lng) { $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false'; $json = @file_get_contents($url); $data=json_decode($json); $status = $data->status; if($status=="ok") { return $data->results[0]->formatted_address; } else { return false; } } $lat= 26.754347; //latitude $lng= 81.001640; //longitude $address= getaddress($lat,$lng); if($address) { echo $address; } else { echo "not found"; } ?>
after lot of searching , trial , error managed needed working. if there more efficient way i'd love feedback.
<?php function getaddress($lat, $lon) { $url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=". $lat.",".$lon."&sensor=false"; $json = @file_get_contents($url); $data = json_decode($json); $status = $data->status; $address = ''; if($status == "ok"){ foreach($data->results[0]->address_components $address_component) { if(in_array('street_number', $address_component->types)) { $street_number = $address_component->long_name; } if(in_array('route', $address_component->types)) { $route = $address_component->long_name; } } } return $street_number." ".$route; } echo getaddress($lat,$lon); ?>
Comments
Post a Comment