以下是我的应用程序中的相关代码......
<?
$jsonData = file_get_contents($url);
$data = json_decode($jsonData, TRUE);
$lat = $data['results']['0']['geometry']['location']['lat'];
$lng = $data['results']['0']['geometry']['location']['lng'];
$formattedAddress = $data['results']['0']['formatted_address'];
$acomp = $data['results']['0']['address_components'];
foreach ($acomp as $acompArray) {
if (in_array("neighborhood", $acompArray["types"])) {
$neighborhood = $acompArray["long_name"];
}
}
$acomp = $data['results']['0']['address_components'];
foreach ($acomp as $acompArray) {
if (in_array("neighborhood", $acompArray["types"])) {
$neighborhood = $acompArray["long_name"];
}
}
?>
以下是来自 Google 地理编码器 API 的 JSON 响应(其中一个损坏的示例)
{
"results" : [
{
"address_components" : [
{
"long_name" : "3900",
"short_name" : "3900",
"types" : [ "street_number" ]
},
{
"long_name" : "Winchell Avenue",
"short_name" : "Winchell Ave",
"types" : [ "route" ]
},
{
"long_name" : "Oakland/Winchell",
"short_name" : "Oakland/Winchell",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Kalamazoo",
"short_name" : "Kalamazoo",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Kalamazoo",
"short_name" : "Kalamazoo",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Michigan",
"short_name" : "MI",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "49008",
"short_name" : "49008",
"types" : [ "postal_code" ]
}
],
---A LOT MORE BUT DELETED THE IRRELEVANT PORTIONS---
}
问题似乎发生在“奥克兰/温切尔”附近,我的理论是它包含一个“/”,这似乎使它什么都不返回......我该如何解决这个问题?