我对 javascript 和 googlemaps api 都是全新的,但过去几天在这里得到了一些很大的帮助,并且使我的网站取得了很大进展(谢谢!)。现在,我有一个新问题。我想要做的是将一组标记从数据库加载到地图,然后允许用户将值输入到表单中以过滤标记(例如,放置最小和最大长度字段)。
这是我的代码,我希望它可以理解:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
//<![CDATA[
var icons = {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.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(51.5100, 0.0000),
zoom: 10,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
//min and max ride lengths sent from form
var minlength = document.getElementById("minlength").value;
var maxlength = document.getElementById("maxlength").value;
// Change this depending on the name of your PHP file
downloadUrl("phpsqlajax_genxml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var length = markers[i].getAttribute("length");
if (minlength < length && length < maxlength) {
var date = markers[i].getAttribute("date");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>Date: " + date + "</b> <br/>Length: " + length;
var icon = icons;
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>
</head>
<link rel="stylesheet" type="text/css" href="style.css" />
<body onload="load()">
<div id="container">
<div id="maincontent">
<form name="findride">
Min length: <input type="number" id="minlength">(miles)<br>
Max length: <input type="number" id="maxlength">(miles)<br>
<input type="button" value="Update" onClick="load()"/>
<div id="map"></div>
</div>
</div>
我遇到的问题是,在用户填写字段并更新之前,地图不显示点(我尝试设置一些初始 maxlength 和 minlength 值,但这也不起作用)。用户输入此数据后,将显示点,但地图不断重新居中 - 我希望它保持在同一位置,只是让标记发生变化。
好吧,也许这有点超出我的能力范围,但是如果可以简单地完成,我将非常感谢您的帮助。