我正在尝试建立一个位置数据库来为我的非营利组织填写 FAFSA,并将它们放在带有自定义标记图像的谷歌地图上,但似乎无法显示标记。
这是创建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 中提取的字段并添加了自定义图标图像。
关于如何让这些标记出现的任何想法?地图显示了我选择的中心和缩放,但没有标记。