Google jsonから$ street、$ city、$ countryの文字列を取得しようとしています。それは私の自宅の住所で動作します: http://maps.googleapis.com/maps/api/geocode/json?latlng=52.108662,6.307370&sensor=true
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lng."&sensor=true";
$data = @file_get_contents($url);
$jsondata = json_decode($data,true);
if(is_array($jsondata) && $jsondata['status'] == "OK")
{
$city = $jsondata['results']['0']['address_components']['2']['long_name'];
$country = $jsondata['results']['0']['address_components']['5']['long_name'];
$street = $jsondata['results']['0']['address_components']['1']['long_name'];
}
ただし、次の例のように配列内により多くのデータがある別のアドレスの場合: http://maps.googleapis.com/maps/api/geocode/json?latlng=52.154184,6.199592&sensor=true それはありませんjson配列にはさらに多くのデータがあり、州を国にするため、機能しません。
必要なタイプ(long_name)を選択するにはどうすればよいですか?
ジオコードJSONからの出力例:
{
"results" : [
{
"address_components" : [
{
"long_name" : "89",
"short_name" : "89",
"types" : [ "street_number" ]
},
{
"long_name" : "Wieck De",
"short_name" : "Wieck De",
"types" : [ "establishment" ]
},
{
"long_name" : "Industrieweg",
"short_name" : "Industrieweg",
"types" : [ "route" ]
},
{
"long_name" : "Zutphen",
"short_name" : "Zutphen",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Zutphen",
"short_name" : "Zutphen",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Gelderland",
"short_name" : "GE",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Nederland",
"short_name" : "NL",
"types" : [ "country", "political" ]
},
{
"long_name" : "7202 CA",
"short_name" : "7202 CA",
"types" : [ "postal_code" ]
}
私は自分で修正したと思います。これにより私のコード:
// street
foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
if (in_array("route", $address["types"])) {
$street = $address["long_name"];
}
}
}
// city
foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
if (in_array("locality", $address["types"])) {
$city = $address["long_name"];
}
}
}
// country
foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
if (in_array("country", $address["types"])) {
$country = $address["long_name"];
}
}
}
データを連想配列に変換して、次のように操作できます
$data = array();
foreach($jsondata['results']['0']['address_components'] as $element){
$data[ implode(' ',$element['types']) ] = $element['long_name'];
}
print_r($data);
echo 'route: ' . $data['route'] . "\n";
echo 'country: ' . $data['country political'];
あなたのコードは完全に優れていますが、foreachループを繰り返すのではなく、1 foreach内でスイッチを使用する方が良いでしょうか?これがまったく同じ配列を解析する方法です:
$location = array();
foreach ($result['address_components'] as $component) {
switch ($component['types']) {
case in_array('street_number', $component['types']):
$location['street_number'] = $component['long_name'];
break;
case in_array('route', $component['types']):
$location['street'] = $component['long_name'];
break;
case in_array('sublocality', $component['types']):
$location['sublocality'] = $component['long_name'];
break;
case in_array('locality', $component['types']):
$location['locality'] = $component['long_name'];
break;
case in_array('administrative_area_level_2', $component['types']):
$location['admin_2'] = $component['long_name'];
break;
case in_array('administrative_area_level_1', $component['types']):
$location['admin_1'] = $component['long_name'];
break;
case in_array('postal_code', $component['types']):
$location['postal_code'] = $component['long_name'];
break;
case in_array('country', $component['types']):
$location['country'] = $component['long_name'];
break;
}
}
郵便番号を使用して住所を検索する場合、Google MAP APIを使用して最近通り、都市、国を生成したので、コードは次のとおりです。
$search_code = urlencode($postcode);
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $search_code . '&sensor=false';
$json = json_decode(file_get_contents($url));
if($json->results == []){
return '';
}
$lat = $json->results[0]->geometry->location->lat;
$lng = $json->results[0]->geometry->location->lng;
//Now build the actual lookup
$address_url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' . $lat . ',' . $lng . '&sensor=false';
$address_json = json_decode(file_get_contents($address_url));
$address_data = $address_json->results[0]->address_components;
//return $address_data = $address_json->results[0]->formatted_address;
$street = str_replace('Dr', 'Drive', $address_data[1]->long_name);
$town = $address_data[2]->long_name;
$county = $address_data[3]->long_name;
return $street.', '. $town. ', '.$county;
JMESpath http://jmespath.org/ のようなセットパーサーの仕事のように見えます
配列を考える
_{
"locations": [
{"name": "Seattle", "state": "WA"},
{"name": "New York", "state": "NY"},
{"name": "Bellevue", "state": "WA"},
{"name": "Olympia", "state": "WA"}
]
}
_
次のJMESPath:
locations[?state == 'WA'].name | sort(@) | {WashingtonCities: join(', ', @)}
収量
_{
"WashingtonCities": "Bellevue, Olympia, Seattle"
}
_
あなたはあなたのケースのために書き直す必要があるでしょうが、あなたはこの言語がどれほど強力であるかという考えを得るでしょう。 composer
を使用して、PHPのJMESPath実装をインストールできます。