arrays - PHP - Returning values from Google Maps API -
i wondering if help me out bit of code have.
i have function follows:
function getaddresscomponent($lat, $lng, $field) { $returnvalue = '-'; $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false'; $data = @file_get_contents($url); $json = json_decode($data, true); if (isset($json['results'])) { foreach($json['results'] $result) { foreach ($result['address_components'] $address_component) { $types = $address_component['types']; if (in_array($field, $types) && sizeof($types) == 1) { $returnvalue = $address_component['short_name']; } } } } homecoming $returnvalue; }
i phone call function this:
$returnaddress = $this->getaddresscomponent( -34.872693, 138.490391, 'administrative_area_level_1' );
if run function administrative_area_level_1 type, returns nothing, if utilize $url , come in lat , lng in url returns results type... array looks so.
{ "results" : [ { "address_components" : [ { "long_name" : "22", "short_name" : "22", "types" : [ "street_number" ] }, { "long_name" : "blue-sails court", "short_name" : "blue-sails ct", "types" : [ "route" ] }, { "long_name" : "west lakes", "short_name" : "west lakes", "types" : [ "locality", "political" ] }, { "long_name" : "south australia", "short_name" : "sa", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "australia", "short_name" : "au", "types" : [ "country", "political" ] }, { "long_name" : "5021", "short_name" : "5021", "types" : [ "postal_code" ] } ],
it returns '-'
locality , country 1 well.
i want homecoming '-'
if doesnt exist, in case exist , doing head in.
any help appreciated.
cheers,
the biggest problem status within if:
as can see in json provided:
"types" : [ "administrative_area_level_1", "political" ]
its sizeof/count
greater zero, contains 2.
so when utilize condition:
if (in_array($field, $types) && sizeof($types) == 1) { // fails // sizeof not equal 1
so basically, no values beingness assigned within $returnvalue
, returns initialization -
.
either alter > 0
or remove it.
php arrays json google-maps
No comments:
Post a Comment