【问题标题】:display issue with markers on google map谷歌地图上的标记显示问题
【发布时间】:2013-05-13 23:13:27
【问题描述】:

我首先要承认我在这方面很新,所以我可能看起来不是最聪明的,但我基本上是想从数据库中获取数据(最好是地址位置)以显示标记针(或任何它们是称为..)。我找到了一个链接,可以引导我完成整个过程,但是当我按照页面告诉我的操作进行操作时,数据标记不会显示在地图所在的页面上。我猜想显示数据的 html 文件出了点​​问题,因为当我将数据排序到其中一个功能页面中的 xml 文件时,xml 数据会从数据库中显示出来。这是链接。 https://developers.google.com/maps/articles/phpsqlajax_v3

如果能帮助我理解我做错了什么,我将不胜感激。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps AJAX + mySQL/PHP Example</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
        type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[

var customIcons = {
  restaurant: {
    icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  },
  bar: {
    icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  }
};

function load() {
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(47.6145, -122.3418),
    zoom: 13,
    mapTypeId: 'roadmap'
  });
  var infoWindow = new google.maps.InfoWindow;

  // Change this depending on the name of your PHP file
  downloadUrl("phpsqlajax_genxml.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var name = markers[i].getAttribute("name");
      var address = markers[i].getAttribute("address");
      var type = markers[i].getAttribute("type");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = "<b>" + name + "</b> <br/>" + address;
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow
      });
      bindInfoWindow(marker, map, infoWindow, html);
    }
  });
}

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}

function downloadUrl(url, callback) {
  var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

function doNothing() {}

//]]>
</script>
</head>

<body onload="load()">
<div id="map" style="width: 500px; height: 300px"></div>
</body>
</html>

这里是把数据库中的数据转成xml文件的文件

require("phpsqlajax_dbinfo.php"); 

// Start XML file, create parent node

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node); 

// Opens a connection to a MySQL server

$connection=mysql_connect ($hostname, $username, $password);
if (!$connection) {  die('Not connected : ' . mysql_error());} 

// Set the active MySQL database

$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
} 

// Select all the rows in the markers table

$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {  
die('Invalid query: ' . mysql_error());
} 

header("Content-type: text/xml"); 

// Iterate through the rows, adding XML nodes for each

while ($row = @mysql_fetch_assoc($result)){  
// ADD TO XML DOCUMENT NODE  
$node = $dom->createElement("marker");  
$newnode = $parnode->appendChild($node);   
$newnode->setAttribute("name",$row['name']);
$newnode->setAttribute("address", $row['address']);  
$newnode->setAttribute("lat", $row['lat']);  
$newnode->setAttribute("lng", $row['lng']);  
$newnode->setAttribute("type", $row['type']);
} 

echo $dom->saveXML();

?>

这是似乎有问题的文件

// Start XML file, create parent node
$doc = new DOMDocument("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);

// Opens a connection to a MySQL server
$connection=mysql_connect ($hostname, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}

// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $doc->create_element("marker");
$newnode = $parnode->append_child($node);

$newnode->set_attribute("name", $row['name']);
$newnode->set_attribute("address", $row['address']);
$newnode->set_attribute("lat", $row['lat']);
$newnode->set_attribute("lng", $row['lng']);
$newnode->set_attribute("type", $row['type']);
}

$xmlfile = $doc->dump_mem();
echo $xmlfile;

?>

感谢您的任何帮助或见解!

【问题讨论】:

  • 您的 XML 有效吗?可以提供网址吗?
  • 我更新了帖子:)
  • @noob123,尝试从mysql_fetch_assoc 中删除@ 并将您的error_reporting 设置为error_reporting(E_ALL ^ E_NOTICE),这样您就可以查看是否有任何错误并反馈;-)
  • @Guy,我再次更新了帖子,我在浏览器中运行了第三个文件,我收到了这个错误:致命错误:“在 C 中调用未定义的方法 DOMDocument::create_element(): \Program Files (x86)\EasyPHP-12.1\www\maps\phpsqlajax_genxml.php 在第 6 行"。所以这显然是错误所在。
  • 是的,createElement 不是 create_element :-) 也适用于 appendChildsetAttribute

标签: php database google-maps google-maps-markers


【解决方案1】:

create_element, append_child and set_attribute 更改为createElement, appendChild and setAttribute

删除dump_mem并将echo $xml更改为echo $xml-&gt;saveXml();

这应该可以解决问题。

【讨论】:

  • 对不起,听起来我没有经验,但我应该删除“$xmlfile = $doc->dump_mem();”这一行还是只是“dump_mem”部分?对此有点困惑,因为 doc 变量将进入转储内存
  • 更改所有这些后,我在此文件上收到此错误:“此页面包含以下错误:第 2 行第 1 列错误:文档末尾的额外内容以下是渲染到第一个错误为止的页面。”
【解决方案2】:

我已经解决了!我只需要将我的 while 循环更改为: 而 ($row = @mysql_fetch_assoc($result)){
// 添加到 XML 文档节点
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name",$row['name']); $newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
} 谢谢大家的帮助!!

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 2011-04-04
    • 2021-07-07
    相关资源
    最近更新 更多