我正在使用谷歌地图 API V3 使用 lat/lng 发出请求,期望得到返回的完整地址数据。但是,我的 PHP 程序有时无法获取例如返回的邮政编码。我对返回的内容做了一个 var_dump。我看到一个额外的对象元素,这可能是我不确定的原因。我完全有可能对 PHP 中的对象如何工作一无所知。这是感兴趣区域的 var_dump 输出。第一个示例确实将邮政编码返回到发出请求的我的 PHP 程序,而第二个示例未能这样做并给出“尝试获取非对象的属性...”的错误。提前感谢您查看此内容和您的有用评论。
这是从 Google Map API V3 返回的 $xml 的 var_dump,它不起作用并且无法正确返回邮政编码:
object(SimpleXMLElement)#1 (1) {
["Response"]=>
object(SimpleXMLElement)#2 (3) {
["name"]=>
string(24) "40.74005999,-73.99718229"
["Status"]=>
object(SimpleXMLElement)#3 (2) {
["code"]=>
string(3) "200"
["request"]=>
string(7) "geocode"
}
["Placemark"]=>
object(SimpleXMLElement)#4 (5) {
["@attributes"]=>
array(1) {
["id"]=>
string(2) "p1"
}
["address"]=>
string(38) "136 W 17th St, New York, NY 10011, USA"
["AddressDetails"]=>
object(SimpleXMLElement)#5 (2) {
["@attributes"]=>
array(1) {
["Accuracy"]=>
string(1) "8"
}
["Country"]=>
object(SimpleXMLElement)#8 (3) {
["CountryNameCode"]=>
string(2) "US"
["CountryName"]=>
string(3) "USA"
["AdministrativeArea"]=>
object(SimpleXMLElement)#9 (2) {
["AdministrativeAreaName"]=>
string(2) "NY"
["SubAdministrativeArea"]=>
object(SimpleXMLElement)#10 (2) {
["SubAdministrativeAreaName"]=>
string(8) "New York"
["Locality"]=>
object(SimpleXMLElement)#11 (2) {
["LocalityName"]=>
string(8) "New York"
["DependentLocality"]=>
object(SimpleXMLElement)#12 (3) {
["DependentLocalityName"]=>
string(9) "Manhattan"
["Thoroughfare"]=>
object(SimpleXMLElement)#13 (1) {
["ThoroughfareName"]=>
string(13) "136 W 17th St"
}
["PostalCode"]=>
object(SimpleXMLElement)#14 (1) {
["PostalCodeNumber"]=>
string(5) "10011"
}
}
}
}
}
}
}
["ExtendedData"]=>
object(SimpleXMLElement)#6 (1) {
["LatLonBox"]=>
object(SimpleXMLElement)#8 (1) {
["@attributes"]=>
array(4) {
["north"]=>
string(10) "40.7414089"
["south"]=>
string(10) "40.7387109"
["east"]=>
string(11) "-73.9958332"
["west"]=>
string(11) "-73.9985312"
}
}
}
["Point"]=>
object(SimpleXMLElement)#7 (1) {
["coordinates"]=>
string(24) "-73.9971822,40.7400599,0"
}
}
}
}
上面有这些错误:
PHP Notice: Trying to get property of non-object in try.php on line 38
PHP Notice: Trying to get property of non-object in try.php on line 41
以下是这些行的 PHP 代码:
$status = $xml->Response->Status->code;
// Country
$country = $xml->Response->Placemark->AddressDetails->Country;
$country_code = $country->CountryNameCode;
$country_name = $country->CountryName;
// Address
$address_line = $xml->Response->Placemark->address;
// Street address
$Locality = $country->AdministrativeArea->Locality;
// var_dump($Locality);
$street = $country->AdministrativeArea->Locality->Thoroughfare->ThoroughfareName;
$city = $country->AdministrativeArea->Locality->LocalityName;
$state = $country->AdministrativeArea->AdministrativeAreaName;
$zip_code = $country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber;
第 41 行是带有 $zip_code 的最后一行代码,第 38 行是以 $street 开头的代码行。
您会注意到上面包含一个 ["SubAdministrativeArea"]=> ,下面的工作示例没有。这有关系吗?
object(SimpleXMLElement)#1 (1) {
["Response"]=>
object(SimpleXMLElement)#2 (3) {
["name"]=>
string(24) "40.74445606,-73.97495072"
["Status"]=>
object(SimpleXMLElement)#3 (2) {
["code"]=>
string(3) "200"
["request"]=>
string(7) "geocode"
}
["Placemark"]=>
object(SimpleXMLElement)#4 (5) {
["@attributes"]=>
array(1) {
["id"]=>
string(2) "p1"
}
["address"]=>
string(38) "317 E 34th St, New York, NY 10016, USA"
["AddressDetails"]=>
object(SimpleXMLElement)#5 (2) {
["@attributes"]=>
array(1) {
["Accuracy"]=>
string(1) "8"
}
["Country"]=>
object(SimpleXMLElement)#8 (3) {
["CountryNameCode"]=>
string(2) "US"
["CountryName"]=>
string(3) "USA"
["AdministrativeArea"]=>
object(SimpleXMLElement)#9 (2) {
["AdministrativeAreaName"]=>
string(2) "NY"
["Locality"]=>
object(SimpleXMLElement)#10 (3) {
["LocalityName"]=>
string(8) "New York"
["Thoroughfare"]=>
object(SimpleXMLElement)#11 (1) {
["ThoroughfareName"]=>
string(13) "317 E 34th St"
}
["PostalCode"]=>
object(SimpleXMLElement)#12 (1) {
["PostalCodeNumber"]=>
string(5) "10016"
}
}
}
}
}
["ExtendedData"]=>
object(SimpleXMLElement)#6 (1) {
["LatLonBox"]=>
object(SimpleXMLElement)#8 (1) {
["@attributes"]=>
array(4) {
["north"]=>
string(10) "40.7458050"
["south"]=>
string(10) "40.7431070"
["east"]=>
string(11) "-73.9736017"
["west"]=>
string(11) "-73.9762997"
}
}
}
["Point"]=>
object(SimpleXMLElement)#7 (1) {
["coordinates"]=>
string(24) "-73.9749507,40.7444560,0"
}
}
}
}
这是用于输出 $zip_code 对于第一个示例为空的代码行,而对于第二个示例,它确实按预期工作。
echo "===================================\n";
echo "Status code is: " . $status . "\n";
echo "address line is:" . $address_line . "\n";
echo "=+=+=" . "\n";
echo "street is: " . $street . "\n";
echo "city is: " . $city . "\n";
echo "state is: " . $state . "\n";
echo "zip code is: " . $zip_code . "\n";
echo "country code is: " . $country_code . "\n";
echo "country name is: " . $country_name. "\n";
此示例中使用的不同位置均位于纽约市。我不知道为什么一个在工作而另一个不在工作,希望我看到它正在返回 ["SubAdministrativeArea"]。Google map API V3 不应该为同一区域返回相同格式的信息吗?这甚至是我遇到问题的一个因素吗?我没有正确处理对象和元素吗?如果是这样,请赐教,因为我被困在这一点上。
或者我应该检查谷歌地图 API V3 可能返回(或更多?)的两种不同情况?谢谢!