我試圖做一個數據庫的位置來填補我的非盈利FAFSA,並把它們放在谷歌地圖與自定義標記圖像,但似乎不能顯示標記。谷歌教程「使用PHP/MySQL與谷歌地圖」的標記問題
這裏是創建XML的PHP:
<?php
require("mapdbinfo.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 ("Localhost", $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());}
$query = "SELECT * FROM locations 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']);
$newnode->setAttribute("date", $row['date']);
$newnode->setAttribute("cord", $row['cord']);
$newnode->setAttribute("cord_info", $row['cord_info']);
}
echo $dom->saveXML();
?>
這裏是Java腳本:
function load() {
var location_icon = new google.maps.MarkerImage('images/FAFSA_Logo_icon.png');
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(35.105305, -106.628014),
zoom: 9,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
}
downloadUrl("mapmysql.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 + "<b> Cordinator </b>" + cord + cord_info;
var icon = location_icon;
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon
});
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);
}
我想不通爲什麼他們標記沒有顯示出來。該文件位於該圖像文件夾中,但是,對於我來說,有很多地方可能會錯誤地使用我當前的知識進行故障排除。我幾乎複製並粘貼了谷歌示例中的代碼,除了添加一些字段以從XML中拉出並添加了自定義圖標圖像。
關於如何讓這些標記出現的任何想法?地圖顯示中心和縮放我選擇,但不是標記。
謝謝泡泡!初始化「load」之外的「map」,然後在「load」中放入「downloadUrl」似乎解決了這個問題。我會留下對這個教程的評論,但我不認爲有辦法做到這一點。 爲了安全起見,我還在一切之外初始化了「location_icon」。 – Bryan
不客氣!你有鏈接到教程?我有點好奇。 – Bubbles
https://developers.google.com/maps/articles/phpsqlajax_v3是此教程的鏈接。 – Bryan