php 地理编码函数不返回经度和纬度


php geocode Function not returning longitude and latitude

我想从SQL数据库中获取地址,将它们放入地理编码器函数中以获取每个位置的经度和纬度,然后在将其绘制到地图之前以XML格式显示位置信息。但是,经度和纬度显示为空白。我做错了什么?

 <? $sqlLink = mysqli_connect($host, $sqlusername, $sqlpassword, $db_name);
    $sqlLinkError = mysqli_connect($host, $sqlusername, $sqlpassword, $db_name);
    $mysqli = new mysqli($host, $sqlusername, $sqlpassword, $db_name);
    if ($mysqli->connect_errno)
    {
        print "Error Connecting to Database";
        exit(); 
    }
    if (mysqli_connect_errno())
    {
        print "Error Connecting to Database";
        exit();
    }

function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','&lt;',$htmlStr);
$xmlStr=str_replace('>','&gt;',$xmlStr);
$xmlStr=str_replace('"','&quot;',$xmlStr);
$xmlStr=str_replace("'",'&#39;',$xmlStr);
$xmlStr=str_replace("&",'&amp;',$xmlStr);
return $xmlStr;
}
// Opens a connection to a MySQL server
$connection=mysql_connect ('localhost', $sqlusername, $sqlpassword);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($db_name, $connection);
if (!$db_selected) {
  die ('Can''t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT id, address1, city, state, zipcode FROM client_information LIMIT 0,12";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
    $data_arr = geocode($row['address1']);
    if($data_arr){
        $latitude = $data_arr[0];
        $longitude = $data_arr[1];
        $formatted_address = $data_arr[2];
    }
    else{
        echo "Error";
    }
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
   echo 'address="' . parseToXML($row['address1']) . '" ';
  echo 'lat="' . $latitude . '" ';
  echo 'lng="' . $longitude . '" ';
  echo '/>';


}
// End XML file
echo '</markers>';
?>
<?
// function to geocode address, it will return false if unable to geocode address
function geocode($address){
    // url encode the address
    $address = urlencode($address);
    // google map geocode api url
    $url = "http://maps.google.com/maps/api/geocode/json?address={$address}";
    // get the json response
    $resp_json = file_get_contents($url);
    // decode the json
    $resp = json_decode($resp_json, true);
    // response status will be 'OK', if able to geocode given address 
    if($resp['status']=='OK'){
        // get the important data
        $lati = $resp['results'][0]['geometry']['location']['lat'];
        $longi = $resp['results'][0]['geometry']['location']['lng'];
        $formatted_address = $resp['results'][0]['formatted_address'];
        // verify if data is complete
        if($lati && $longi && $formatted_address){
            // put the data in the array
            $data_arr = array();            
            array_push(
                $data_arr, 
                    $lati, 
                    $longi, 
                    $formatted_address
                );
            return $data_arr;
        }else{
            return false;
        }
    }else{
        return false;
    }
}
?>

行: $url = "http://maps.google.com/maps/api/geocode/json?address={$address}";

我正在使用

我认为现在已经过时了,我将其修改为现在正在使用的那个

$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key="MY API KEY";

它现在可以工作了。

其他任何阅读本文的人因为他们有类似的问题,我将分享我的解决方案,因为我在连接到 Google 地理编码时使用类似技术时遇到了类似的问题。 事实证明,如果您使用的是PHP版本5.4,那么$resp_json = file_get_contents($url);将不会返回对象。 它将抛出错误而不是返回 false。 升级到5.6版为我修复了它。