因此,我正在尝试制作一张地图,以便在我旅行时显示特定的纪念碑。我希望它在我在屏幕上移动地图时更新可用的纪念碑。 我正在使用带有GET参数的php页面来返回我的数据库中可用的点数。 我目前已将它设置为起点,但在拖动地图后我无法弄清楚如何更新它。我希望它能够根据地图的中心进行更新,无论哪里可能是dragend。 这是带有JavaScript的地图页面。
<!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>NGS Dynamic Map with Google Maps</title>
<style>
#main{
height:90%;
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="main">
<div id="map"></div>
</div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(30.27,-98.87),
zoom: 12
});
var infoWindow = new google.maps.InfoWindow;
var NewMapCenter = map.getCenter();
var vlat = NewMapCenter.lat();
var vlong = NewMapCenter.lng();
// Change this depending on the name of your PHP or XML file
downloadUrl('makePoints.php?lat=' + vlat + '&long=' + vlong, function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var id = markerElem.getAttribute('id');
var name = markerElem.getAttribute('name');
var county = markerElem.getAttribute('county');
var state = markerElem.getAttribute('state');
var elevation = markerElem.getAttribute('elevation');
var data_source = markerElem.getAttribute('data_source');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = "PID# " + name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = county + " County"
infowincontent.appendChild(text);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = elevation + " (US Survey Ft.)"
infowincontent.appendChild(text);
infowincontent.appendChild(document.createElement('br'));
var newlink = document.createElement('a');
newlink.textContent = data_source
newlink.setAttribute('target', '_new');
newlink.setAttribute('href', data_source);
infowincontent.appendChild(newlink);
infowincontent.appendChild(document.createElement('br'));
var marker = new google.maps.Marker({
map: map,
position: point
});
marker.addListener('click', function() {
infoWindow.setContent(infowincontent);
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>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD_wB5WVi2nupVm9WzQ9C8Px_iYB1JZJv0&callback=initMap">
</script>
</body>
</html>
我想我可以保留我的downloadUrl参数的变量并在dragend事件上更新它们,这也会重新初始化调用和处理downloadUrl()数据的函数。然后地图会更新,但我不确定这是实现它的正确方法。
此外,downloadUrl页面只需要通过GET传递的纬度/经度并访问我的数据库,然后抓住经过它的纬度/经度半径10英里范围内的所有内容。 php返回XML。 我真的不太了解.js了解如何使这项工作。任何想法都将不胜感激。
答案 0 :(得分:1)
你需要的是地图上dragend的事件监听器,重复你的PHP文件的ajax请求。在其自己的函数中调用该ajax请求。将map
变量设为全局变量,以便可以在两个函数中访问它。
这样的事情:
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(30.27, -98.87),
zoom: 12
});
getPoints(30.27, -98.87);
map.addListener('dragend', function() {
var NewMapCenter = this.getCenter();
var vlat = NewMapCenter.lat();
var vlong = NewMapCenter.lng();
getPoints(vlat, vlong);
});
}
function getPoints(vlat, vlong) {
downloadUrl('makePoints.php?lat=' + vlat + '&long=' + vlong, function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
var infoWindow = new google.maps.InfoWindow();
markers.forEach(function(markerElem) {
var id = markerElem.getAttribute('id');
var name = markerElem.getAttribute('name');
var county = markerElem.getAttribute('county');
var state = markerElem.getAttribute('state');
var elevation = markerElem.getAttribute('elevation');
var data_source = markerElem.getAttribute('data_source');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));
var infowincontent = // ... etc
var marker = new google.maps.Marker({
map: map,
position: point
});
marker.addListener('click', function() {
infoWindow.setContent(infowincontent);
infoWindow.open(map, marker);
});
});
});
}