如何根据新位置更新 LAT/LONG 变量并刷新引脚



我正在尝试制作一张地图,当我在州内旅行时,该地图将显示特定的古迹。我希望它在我移动屏幕上的地图时更新可用的纪念碑。 我正在使用带有GET参数的PHP页面来返回数据库中可用的点。 我目前已将其设置在起点上,但是在拖动地图后,我不知道如何使其更新。我希望它根据地图的中心进行更新,无论在拖累点在哪里。 这是带有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来理解如何做到这一点。任何想法将不胜感激。

你需要的是一个事件侦听器,用于映射上的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);
});
});
}); 
}   

最新更新