php - 谷歌地图 API 和 XML

标签 php mysql xml database google-maps

我一直在尝试学习如何使用他们的在线教程在谷歌地图上放置多个标记 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('<','&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
$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/

相关文章:

javascript - 在成功的 AJAX 调用和 php echo 的响应后附加数据并添加类

php - 如何使用 php 中准备好的语句插入数据库

MySQL 例程未显示在 Workbench 对象浏览器中

mysql - 如何将 mysql 结果分配给变量(在 C 中)

java - 尽可能晚地初始化 Spring Bean

ios - 如何在 Swift 中将文件写入位于 Apple 的文件应用程序中的文件夹

xml - 如何使用 Groovy 在 xml 中插入/移动/删除节点?

php - 使用 PHP/Laravel 查询循环

php - Google_Service_OAuth2 在 PHP 中是 'Undefined type'

php - ORDER BY 应用在 DISTINCT 之前还是之后?