2016-02-26 72 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... 
} 
相关问题