gpt4 book ai didi

javascript - 实时更新标记

转载 作者:行者123 更新时间:2023-12-05 01:17:44 24 4
gpt4 key购买 nike

我需要从 json 文件中获取新位置,该文​​件将定期更新,以便在 map 上更新它而无需重复重新加载整个页面。不使用 Ajax 怎么办

                 if (GBrowserIsCompatible()) {
//==add controls
var map = new GMap(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(-29.870879, 30.977258),15);

var htmls = [];
var i = 0;

//create marker and set up infoWindow
function createMarker(point,ID,name) {
var marker = new GMarker(point);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(ID+"<br/>Name: " +name);

});

return marker;
}



process_Data = function(doc) {
//parse json file
var jsonData = eval('(' + doc + ')');

// ======== Plots the markers on Google Maps============

for (var i=0; i<jsonData.markers.length; i++) {
var point = new GLatLng(jsonData.markers[i].lat, jsonData.markers[i].lng);
var marker = createMarker(point,jsonData.markers[i].ID,jsonData.markers[i].name);
map.addOverlay(marker);

}
}

GDownloadUrl("points.json", process_Data);

}

最佳答案

var marker;

// every 10 seconds
setInterval(updateMarker,10000);

function updateMarker() {
$.post('/path/to/server/getPosition',{}, function(json) {
var LatLng = new google.maps.LatLng(json.latitude, json.longitude);
marker.setPosition(LatLng);
});
}

关于javascript - 实时更新标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9736508/

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