[英]markers not being displayed on google maps from XML file
So I'm trying to create markers on to a google map with queried lat/long from a database and saved onto a XML file. 因此,我正在尝试使用数据库中的经纬度/经度查询在Google地图上创建标记,并将其保存到XML文件中。 I'm trying to grab the locations from the XML file and into a new google marker, but am unable to display any markers on the map. 我正在尝试从XML文件中获取位置并放入新的Google标记中,但是无法在地图上显示任何标记。
My HTML/JavaScript 我的HTML / JavaScript
//Test for markers
<!DOCTYPE html>
<html>
<head>
<style>
#map {
width: 1200px;
height: 900px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(38.0400, -122.7400);
var mapOptions = {
zoom: 10,
center: myLatlng
}
map = new google.maps.Map(document.getElementById("map"), mapOptions);
}
loadMarkers();
google.maps.event.addDomListener(window, 'load', initialize);
function loadMarkers(){
map.markers = map.markers || []
downloadUrl("schools.xml", function(data) {
var xml = data.responseXML;
markers = xml.documentElement.getElementsByTagName("school");
for(var i = 0; i < markers.length; i++){
var name = markers[i].getAttribute("name");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("long")));
var marker = new google.maps.Marker({
map:map,
position: point,
title:name
});
map.markers.push(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);
}
</script>
</head>
<body onload = "initialize()">
<div id="map"></div>
</body>
</html>
XML File XML文件
<?xml version="1.0"?>
-<Schools>
<school loc="POINT (-122.549936 37.955014)" lat="37.955014" long="-122.549936" county="Marin" zip="94904" state="CA" city="Kentfield" name2="" name="College of Marin"/>
<school loc="POINT (-122.564302 37.969594)" lat="37.969594" long="-122.564302" county="Marin" zip="94960" state="CA" city="San Anselmo" name2="" name="San Francisco Theological Seminary"/>
<school loc="POINT (-122.514654 37.980014)" lat="37.980014" long="-122.514654" county="Marin" zip="94901" state="CA" city="San Rafael" name2="" name="Dominican University of California"/>
</Schools>
Your code has a javascript error: Uncaught TypeError: Cannot read property 'markers' of undefined
on this line: 您的代码有一个JavaScript错误: Uncaught TypeError: Cannot read property 'markers' of undefined
此行Uncaught TypeError: Cannot read property 'markers' of undefined
:
map.markers = map.markers || []
You are calling the loadMarkers
before the map is defined. 在定义映射之前,您正在调用loadMarkers
。 Call it inside the initialize routine. 在初始化例程中调用它。
function initialize() {
var myLatlng = new google.maps.LatLng(38.0400, -122.7400);
var mapOptions = {
zoom: 10,
center: myLatlng
}
map = new google.maps.Map(document.getElementById("map"), mapOptions);
loadMarkers();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.