Php 在谷歌地图中设置纬度和经度-未定义的结果
当前正在home.php上使用downloadURL()下载xml.php文件。从数据库创建xml文档以在地图上显示标记。之前工作正常,但现在我收到了关于geoCode/lat/long的错误。有什么建议吗?FireFox错误:未通过结果定义偏移量[0]Php 在谷歌地图中设置纬度和经度-未定义的结果,php,json,google-maps,Php,Json,Google Maps,当前正在home.php上使用downloadURL()下载xml.php文件。从数据库创建xml文档以在地图上显示标记。之前工作正常,但现在我收到了关于geoCode/lat/long的错误。有什么建议吗?FireFox错误:未通过结果定义偏移量[0] <?php include 'connect.php'; if(isset($_SESSION['user_name'])) { header('Location: home.php'); } $que
<?php
include 'connect.php';
if(isset($_SESSION['user_name']))
{
header('Location: home.php');
}
$query = "SELECT `acc_id`,`acc_name`,`acc_address`,acc_zip FROM `account_acc` ";
$result = mysqli_query($connection,$query) or die(mysql_error());
$doc = new DomDocument('1.0');
$node = $doc->createElement("markers");
$parnode = $doc->appendChild($node);
header('Content-type: text/xml');
while($row = mysqli_fetch_array($result))
{
$node = $doc->createElement("marker");
$newnode = $parnode->appendChild($node);
$address = $row['acc_zip'];
$prepAddr = str_replace(' ','+',$address);
$geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?key=API&address='.$prepAddr);
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
$newnode->setAttribute("name", $row['acc_name']);
$newnode->setAttribute("accid", $row['acc_id']);
$newnode->setAttribute("address", $row['acc_address']);
$newnode->setAttribute("zip", $row['acc_zip']);
$newnode->setAttribute("lat", $lat);
$newnode->setAttribute("long", $long);
}
print $doc->saveXML();
createElement(“标记”);
$newnode=$parnode->appendChild($node);
$address=$row['acc_-zip'];
$prepAddr=str_replace(“,+”,$address);
$geocode=文件\u获取\u内容('https://maps.google.com/maps/api/geocode/json?key=API&address=“.$prepAddr);
$output=json_decode($geocode);
$lat=$output->结果[0]->几何体->位置->lat;
$long=$output->results[0]->geometry->location->lng;
$newnode->setAttribute(“名称”,$row['acc_name']);
$newnode->setAttribute(“accid”,$row['acc_id']);
$newnode->setAttribute(“地址”,$row['acc_address']);
$newnode->setAttribute(“zip”,$row['acc_-zip']);
$newnode->setAttribute(“lat”,$lat);
$newnode->setAttribute(“long”,$long);
}
打印$doc->saveXML();
?>没有位置的lat/lng属性,它们是函数(lat()/lng())。由于无法确定提供的地址(
$address
变量),并且响应包含空的结果,因此您很可能得到错误未定义的偏移量[0]
。对于这种情况,请提供状态代码:
- “OK”表示没有发生错误;已成功解析该地址,并且至少返回了一个地理代码
- “ZERO_RESULTS”表示地理代码已成功,但未返回任何结果。如果向地理编码器传递了
不存在的地址
- “超过限额”表示您超出了配额
- “请求被拒绝”表示您的请求被拒绝
- “无效的_请求”通常表示缺少查询(地址、组件或latlng)
- “未知_错误”表示由于服务器错误而无法处理请求。如果您重试,请求可能会成功
您可以利用它来确定地址是否已解析
示例
$geocode = file_get_contents('https://maps.google.com/maps/api/geocode/json?address=' . $prepAddr);
$output = json_decode($geocode);
if($output->status == "OK"){
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
//the remaining code goes here...
}