我想从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('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$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;
}
}
?>
答案 0 :(得分:1)
行:$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";
它现在有效。
答案 1 :(得分:1)
其他人因为他们有类似问题而阅读此内容,我将分享我的解决方案,因为我在使用类似技术时遇到类似的问题,连接到Google Geocode时。事实证明,如果您使用的是PHP 5.4版,则$resp_json = file_get_contents($url);
将不会返回对象。它会抛出错误而不是返回false。升级到5.6版为我修复了它。