Bonsoir,
J'ai un petit souci pour récupérer la valeur dans un array qui est lui même dans un autre array ... le tout en php.
Mon but étant de récupérer le code postal de coordonnées Gps.
Avec l'outil geocode de google map en bas de page voici ce que j'obtiens avec la fonction suivante :
J'utilise une fonction qui me permet de récupérer tout ce code json
Je peux afficher mes valeurs de retour ici :Code: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; // Recupère si reponse existe via la fonction status de google if($status=="OK") { return $data->results[0]->address_components; } else return false; }
Mais comment retenir que la valeur du code postal ? Et au passage dans une autre variable la valeur du pays ?Code:$lng= 5.1802; $lat= 43.59901; $adresse = getaddress($lat,$lng); print_r($adresse);
Merci d'avance pour votre aide ...
Pour ceux qui voudrait seulement retenir en valeur l'adresse complète voici la fonction :
Code: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; // Recupère si reponse existe via la fonction status de google if($status=="OK") { return $data->results[1]->formatted_address; } else return false; }
Code:{ "results" : [ { "address_components" : [ { "long_name" : "Bois Vieux", "short_name" : "Bois Vieux", "types" : [ "route" ] }, { "long_name" : "Saint-Pantaléon-les-Vignes", "short_name" : "Saint-Pantaléon-les-Vignes", "types" : [ "locality", "political" ] }, { "long_name" : "Drôme", "short_name" : "26", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Rhône-Alpes", "short_name" : "RA", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "France", "short_name" : "FR", "types" : [ "country", "political" ] }, { "long_name" : "26770", "short_name" : "26770", "types" : [ "postal_code" ] } ], "formatted_address" : "Bois Vieux, 26770 Saint-Pantaléon-les-Vignes, France", "geometry" : { "bounds" : { "northeast" : { "lat" : 44.4039069, "lng" : 5.039973 }, "southwest" : { "lat" : 44.40191799999999, "lng" : 5.037302 } }, "location" : { "lat" : 44.4037279, "lng" : 5.037637399999999 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 44.4042614302915, "lng" : 5.039986480291502 }, "southwest" : { "lat" : 44.4015634697085, "lng" : 5.037288519708497 } } }, "types" : [ "route" ] }, { "address_components" : [ { "long_name" : "Saint-Pantaléon-les-Vignes", "short_name" : "Saint-Pantaléon-les-Vignes", "types" : [ "locality", "political" ] }, { "long_name" : "Drôme", "short_name" : "26", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Rhône-Alpes", "short_name" : "RA", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "France", "short_name" : "FR", "types" : [ "country", "political" ] } ], "formatted_address" : "Saint-Pantaléon-les-Vignes, France", "geometry" : { "bounds" : { "northeast" : { "lat" : 44.40888899999999, "lng" : 5.071002 }, "southwest" : { "lat" : 44.3810789, "lng" : 5.013318 } }, "location" : { "lat" : 44.3976309, "lng" : 5.044534 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 44.40888899999999, "lng" : 5.071002 }, "southwest" : { "lat" : 44.3810789, "lng" : 5.013318 } } }, "types" : [ "locality", "political" ] }, { "address_components" : [ { "long_name" : "26770", "short_name" : "26770", "types" : [ "postal_code" ] }, { "long_name" : "Taulignan", "short_name" : "Taulignan", "types" : [ "locality", "political" ] }, { "long_name" : "Drôme", "short_name" : "26", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Rhône-Alpes", "short_name" : "RA", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "France", "short_name" : "FR", "types" : [ "country", "political" ] } ], "formatted_address" : "26770 Taulignan, France", "geometry" : { "bounds" : { "northeast" : { "lat" : 44.513986, "lng" : 5.1070835 }, "southwest" : { "lat" : 44.3812057, "lng" : 4.920115099999999 } }, "location" : { "lat" : 44.4599281, "lng" : 4.997446099999999 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 44.513986, "lng" : 5.1070835 }, "southwest" : { "lat" : 44.3812057, "lng" : 4.920115099999999 } } }, "postcode_localities" : [ "Aleyrac", "Le Pègue", "Montbrison-sur-Lez", "Roche-Saint-Secret-Béconne", "Rousset-les-Vignes", "Saint-Pantaléon-les-Vignes", "Salles-Sous-Bois", "Taulignan" ], "types" : [ "postal_code" ] }, { "address_components" : [ { "long_name" : "Drôme", "short_name" : "26", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Rhône-Alpes", "short_name" : "RA", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "France", "short_name" : "FR", "types" : [ "country", "political" ] } ], "formatted_address" : "Drôme, France", "geometry" : { "bounds" : { "northeast" : { "lat" : 45.343976, "lng" : 5.830446 }, "southwest" : { "lat" : 44.115494, "lng" : 4.6468618 } }, "location" : { "lat" : 44.73118960000001, "lng" : 5.2266675 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 45.343976, "lng" : 5.830446 }, "southwest" : { "lat" : 44.115494, "lng" : 4.6468618 } } }, "types" : [ "administrative_area_level_2", "political" ] }, { "address_components" : [ { "long_name" : "Rhône-Alpes", "short_name" : "RA", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "France", "short_name" : "FR", "types" : [ "country", "political" ] } ], "formatted_address" : "Rhône-Alpes, France", "geometry" : { "bounds" : { "northeast" : { "lat" : 46.519953, "lng" : 7.1855661 }, "southwest" : { "lat" : 44.115493, "lng" : 3.6889129 } }, "location" : { "lat" : 45.1695797, "lng" : 5.4502821 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 46.519953, "lng" : 7.1855661 }, "southwest" : { "lat" : 44.115493, "lng" : 3.6889129 } } }, "types" : [ "administrative_area_level_1", "political" ] }, { "address_components" : [ { "long_name" : "France", "short_name" : "FR", "types" : [ "country", "political" ] } ], "formatted_address" : "France", "geometry" : { "bounds" : { "northeast" : { "lat" : 51.089166, "lng" : 9.560067799999999 }, "southwest" : { "lat" : 41.3423276, "lng" : -5.141227900000001 } }, "location" : { "lat" : 46.227638, "lng" : 2.213749 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 51.089166, "lng" : 8.233549099999999 }, "southwest" : { "lat" : 42.333014, "lng" : -4.795341899999999 } } }, "types" : [ "country", "political" ] } ], "status" : "OK" }
-----