2016-02-26 70 views
0

目前在home.php上使用downloadURL()來下載xml.php文件。從數據庫創建xml文檔以在地圖上顯示標記。工作得很好,但現在我收到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(); 

?>

回答

0

有不位置的緯度/經度的屬性,它們是功能(LAT()/經度())。

0

由於無法確定提供的地址($address變量),並且響應中包含空的results,您很有可能會收到錯誤undefined offset [0]。對於這種情況Google Maps Geocoding API提供Status Codes

  • "OK" indicates that no errors occurred; the address was successfully parsed and at least one geocode was returned.
  • "ZERO_RESULTS" indicates that the geocode was successful but returned no results. This may occur if the geocoder was passed a non-existent address.
  • "OVER_QUERY_LIMIT" indicates that you are over your quota.
  • "REQUEST_DENIED" indicates that your request was denied.
  • "INVALID_REQUEST" generally indicates that the query (address, components or latlng) is missing.
  • "UNKNOWN_ERROR" indicates that the request could not be processed due to a server error. The request may succeed if you try again.

,你可以利用,以確定地址是否已經解決。

$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... 
} 
相關問題