[英]Setting Latitude and Longitude in Google Maps - undefined results
當前在home.php上使用downloadURL()來下載xml.php文件。 從數據庫創建xml doc,以在地圖上顯示標記。 之前工作正常,但現在我收到有關geoCode / lat / long的錯誤。 有什么建議嗎? FireFox錯誤:結果未定義偏移[0]。
<?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();
?>
沒有location的lat / lng屬性,它們是函數(lat()/ lng())。
由於undefined offset [0]
提供的地址( $address
變量),並且響應包含空results
很可能出現錯誤undefined offset [0]
。 在這種情況下, Google Maps Geocoding API提供了Status Codes
:
- “ OK”表示未發生任何錯誤; 該地址已成功解析,並且至少返回了一個地理編碼。
- “ ZERO_RESULTS”表示地理編碼成功,但未返回任何結果。 如果地址解析器傳遞的地址不存在,則可能會發生這種情況。
- “ OVER_QUERY_LIMIT”表示您已超出配額。
- “ REQUEST_DENIED”表示您的請求被拒絕。
- “ INVALID_REQUEST”通常表示缺少查詢(地址,組件或名稱)。
- “ UNKNOWN_ERROR”表示由於服務器錯誤而無法處理該請求。 如果重試,請求可能會成功。
您可以利用它來確定地址是否已解析。
例
$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...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.