我一直在尝试学习如何使用他们的在线教程在谷歌地图上放置多个标记 https://developers.google.com/maps/articles/phpsqlajax_v3 .我正在使用 wamp 2.5 对其进行测试。我已经复制了他们的数据库和代码,并将 php 数据库连接更改为新版本,但我仍然收到错误
This XML file does not appear to have any style information associated with it.
文档树如下所示。
<markers/></em>
这是我从网站上使用的代码。我是否遗漏了什么,为什么会出现此错误?
<?php
require("phpsqlajax_dbinfo.php");
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
$db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if(mysqli_connect_errno()){
echo "Failed to connect to Mysql: " . mysqli_connect_error();
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = $db_connection->query($query);
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)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
?>
最佳答案
我通过将数据库查询编码为 json,将 json 传递给将创建 javascript 数组的 javascript 函数,然后将新创建的数组与 google API 一起使用来解决这个问题。可能不是正确或最简单的方法,但它确实有效。如果有人想要我如何做的例子,我很乐意提供代码示例!
关于php - 谷歌地图 API 和 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30984092/