0

我正在关注如何在 php/mysql 中使用谷歌地图的教程:https ://developers.google.com/maps/articles/phpsqlajax_v3

在 Firefox 中一切正常,但在 Chrome 或资源管理器中没有显示标记。在 chrome 控制台中,我收到以下错误:

未捕获的类型错误:无法读取 null /maps/:35(匿名函数)/maps/:35 request.onreadystatechange 的属性“documentElement”

这是我的代码(与教程中的完全相同):

<!DOCTYPE html >
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>PHP/MySQL & Google Maps Example</title>

 <script type="text/javascript"
 src="http://maps.googleapis.com/maps/api/js?    
key=AIzaSyAGdu4GFzdG3B203KSHApI5hA9HQmGKRrc&sensor=true">
</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("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>


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

</html>

这是输出xml的文件:

<?php  

require("db.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 ('maps', $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"); 

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

while (( $row = mysql_fetch_assoc($result))!==false){

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

 echo $dom->saveXML();

 ?>

非常感谢任何帮助!

4

2 回答 2

1
mapTypeId: 'roadmap'

This is not a valid map type. It should be like this instead:

mapTypeId: google.maps.MapTypeId.ROADMAP

This isn't quite right either:

var infoWindow = new google.maps.InfoWindow;

It should be like this instead:

var infoWindow = new google.maps.InfoWindow();
于 2012-07-10T14:40:18.420 回答
0

除了邓肯已经指出的问题,你不能做

  var icon = customIcons[type] || {};

因为 Javascript 中没有关联数组。var customIcons 被定义为一个对象,因此

var icon = eval('customIcons'+'.'+type) || {};

那么,Marker对象的icon参数应该是一个MarkerImage对象,所以不是

icon: icon.icon,
shadow: icon.shadow,

你应该使用

icon: new google.maps.MarkerImage(icon.icon),
shadow: new google.maps.MarkerImage(icon.shadow),
于 2012-07-10T15:47:27.683 回答