2012-07-10 65 views
0

沒有顯示我下面就如何使用谷歌地圖使用的PHP/MySQL這裏的教程:https://developers.google.com/maps/articles/phpsqlajax_v3谷歌地圖標記在資源管理器或Chrome

一切正常在Firefox,但標記無法在Chrome或顯示資源管理器。在Chrome控制檯我得到他以下錯誤:

遺漏的類型錯誤:無法讀取的特性 'documentElement' 空/地圖/ 35 (匿名函數)/地圖/ 35 request.onreadystatechange

這裏是我的代碼(完全一樣的教程):

<!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(); 

?> 

任何幫助非常感謝!

回答

0

除了問題,鄧肯已經指出,你不能這樣做

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

,因爲Javascript中沒有關聯數組。 VAR customIcons被定義爲一個對象,因此

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

然後,標記對象的圖標參數應爲MarkerImage對象,所以不是

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

應該使用

icon: new google.maps.MarkerImage(icon.icon), 
shadow: new google.maps.MarkerImage(icon.shadow), 
+0

感謝您爲我指出這一點。我已經做出了您所建議的更改。但是,它並沒有解決我的問題。仍然得到相同的錯誤。你有沒有工作腳本? – user1009453 2012-07-10 18:56:28

+1

對不起,但我沒有你的腳本*的工作副本。但是,如果您想發佈一個鏈接到我們可以正確調試的鏈接。請注意,您收到的錯誤引用了行var markers = xml.documentElement.getElementsByTagName(「marker」);所以你的XML可能無法正確加載。 – Marcelo 2012-07-10 19:03:46

+0

我想也許你hade嘗試過相同的教程(上面的鏈接),這就是爲什麼我問。這裏是使用上面發佈的腳本的實時站點:http://gjutveckling.se/maps/,這裏是xml輸出:http://gjutveckling.se/maps/genxml.php感謝您的幫助:) – user1009453 2012-07-10 20:51:06

1
mapTypeId: 'roadmap' 

這不是一個有效的地圖類型。它應該是這樣的,而不是:

mapTypeId: google.maps.MapTypeId.ROADMAP 

這是不完全正確或者:

var infoWindow = new google.maps.InfoWindow; 

它應該是這樣的,而不是:

var infoWindow = new google.maps.InfoWindow(); 
+0

我想你也需要一個身體。 – Pete 2012-07-10 14:41:57

+0

@Pete:是的,我只是把身體部分留下了,但現在我已經把它放在示例代碼中了。鄧肯:我嘗試了你的建議,但標記仍然沒有顯示,控制檯中的錯誤仍然存​​在。 – user1009453 2012-07-10 15:12:08