我正在尝试从地址获取坐标,但是有一个错误,称为未定义的偏移量。实际上array不为null,当我尝试一一获取坐标时,可以毫无问题地获取它。但是,当我尝试一次全部获取时,会弹出错误消息。
$addr = [];
foreach($newHtml as $link){
$htm = pageContent($link);
$paths = new \DOMXPath($htm);
$routes = $paths->query("//body/div[@class='p-main']//table/tr[4]/td");
foreach ($routes as $route) {
$addr = trim($route->nodeValue);
}
$address = urlencode($addr);
$response = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=MY_API_KEY");
$result = json_decode($response);
$lat[] = trim($result->results[0]->geometry->location->lat);
$lng[] = trim($result->results[0]->geometry->location->lng);
}
var_dump($lat, $lng);
响应给出了这个
[“结果”] => array(1){ [0] => object(stdClass)#696(6){ [“ address_components”] => array(8){ [0] => object(stdClass)#684(3){ [“ long_name”] => 字符串(9)“ D21” [“ short_name”] => 字符串(9)“ D21” [“类型”] => array(1){ [0] => string(7)“前提” } } [1] => object(stdClass)#709(3){ [“ long_name”] => 字符串(1)“ 1” [“ short_name”] => 字符串(1)“ 1” [“类型”] => 数组(3){ [0] => string(9)“政治的” [1] => 字符串(11)“ sublocality” [2] => 字符串(19)“ sublocality_level_4” } } [2] => object(stdClass)#714(3){ [“ long_name”] => 字符串(7)“ 5 Chome” [“ short_name”] => 字符串(7)“ 5 Chome” [“类型”] => 数组(3){ [0] => string(9)“政治的” [1] => 字符串(11)“ sublocality” [2] => 字符串(19)“ sublocality_level_3” } } [3] => object(stdClass)#698(3){ [“ long_name”] => 字符串(8)“古岛” [“ short_name”] => 字符串(8)“古岛” [“类型”] => 数组(3){ [0] => string(9)“政治的” [1] => 字符串(11)“ sublocality” [2] => 字符串(19)“ sublocality_level_2” } } [4] => object(stdClass)#680(3){ [“ long_name”] => 字符串(5)“ Suita” [“ short_name”] => 字符串(5)“ Suita” [“类型”] => array(2){ [0] => string(8)“ locality” [1] => string(9)“政治的” } } [5] => object(stdClass)#701(3){ [“ long_name”] => 字符串(16)“大阪府” [“ short_name”] => 字符串(16)“大阪府” [“类型”] => array(2){ [0] => 字符串(27)“ administrative_area_level_1” [1] => string(9)“政治的” } } [6] => object(stdClass)#695(3){ [“ long_name”] => 字符串(5)“日本” [“ short_name”] => 字符串(2)“ JP” [“类型”] => array(2){ [0] => string(7)“国家/地区” [1] => string(9)“政治的” } } [7] => object(stdClass)#699(3){ [“ long_name”] => 字符串(8)“ 565-0874” [“ short_name”] => 字符串(8)“ 565-0874” [“类型”] => array(1){ [0] => 字符串(11)“邮政编码” } } } [“ formatted_address”] => 字符串(69)“日本大阪府吹田市5 Chome-1-D21 Furuedai,日本565-0874” [“ geometry”] => object(stdClass)#690(3){ [“位置”] => object(stdClass)#694(2){ [“ lat”] => 浮动(34.8097093) [“ lng”] => 浮动(135.5119112) } [“ location_type”] => 字符串(7)“ ROOFTOP” [“视口”] => object(stdClass)#689(2){ [“东北”] => object(stdClass)#688(2){ [“ lat”] => 浮动(34.811058280291) [“ lng”] => 浮动(135.51326018029) } [“西南”] => object(stdClass)#692(2){ [“ lat”] => 浮动(34.808360319709) [“ lng”] => 浮动(135.51056221971) } } } [“ place_id”] => 字符串(27)“ ChIJUY4O7kP7AGARu_UB4lJPFWU” [“ plus_code”] => object(stdClass)#687(2){ [“ compound_code”] => 字符串(30)“日本大阪府RG56 + VQ Suita” [“ global_code”] => 字符串(11)“ 8Q6QRG56 + VQ” } [“类型”] => array(2){ [0] => 字符串(13)“建立” [1] => 字符串(17)“ point_of_interest” } }} [“状态”] =>字符串(2)“确定”
答案 0 :(得分:0)
此行导致错误:
$result->results[0]
您需要检查$result->results
是否具有0键。
更新:
要解决您的问题,您需要捕获意外的响应:
$response = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=MY_API_KEY");
$result = json_decode($response);
try {
$lat[] = trim($result->results[0]->geometry->location->lat);
$lng[] = trim($result->results[0]->geometry->location->lng);
} catch (\Exeption $e) {
var_dump($result);
}
发生异常时,您将看到引起问题的响应,然后您需要根据收到的内容添加响应格式检查。
例如,如果您收到以下信息:
{"results":[], "status": "ZERO_RESULTS"}
您的代码应如下所示:
$response = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=MY_API_KEY");
$result = json_decode($response);
if(!empty($result->results)){
$lat[] = trim($result->results[0]->geometry->location->lat);
$lng[] = trim($result->results[0]->geometry->location->lng);
}
另请参阅https://developers.google.com/maps/documentation/geocoding/intro
答案 1 :(得分:0)
尝试更改
$locations = json_decode($response, true);
if(is_array($locations)) {
$lat = $locations['results'][0]['geometry']['location']['lat'];
$lng = $locations['results'][0]['geometry']['location']['lng'];
}
json_decode->将返回对象数组
以第二个参数为真的json_decode作为数组返回