gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:54:29 25 4
gpt4 key购买 nike

我正在尝试制作一张 map ,在我游览该州时显示特定的纪念碑。当我在屏幕上移动 map 时,我希望它能够更新可用的纪念碑。我正在使用带有 GET 参数的 PHP 页面来返回数据库中可用的点。我目前已将其设置在起点上,但我无法弄清楚如何在拖动 map 后使其更新。我希望它根据 map 的中心进行更新,无论它位于拖尾的任何位置。这是带有 JavaScript 的 map 页面:

<!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() 数据的函数。然后 map 就会更新,但我不确定这是实现这一目标的正确方法。

此外,downloadUrl 页面只需获取 GET 传递的纬度/经度并访问我的数据库,然后获取传递给它的纬度/经度 10 英里半径内的所有内容。 PHP 返回 XML。我真的不知道足够的 .js 来理解如何完成这项工作。任何想法将不胜感激。

最佳答案

您需要的是 map 上的拖尾事件监听器,以重复对 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);
});
});
});
}

关于javascript - 如何根据新位置更新 LAT/LONG 变量并刷新引脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45014353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com