我尝试在几秒钟后重新加载页面,并使用包含新Lat Long坐标的变量附加URL。请考虑下面与Leaflet一起使用的代码段:
var latlng = getQueryVariable("latlng");
if(latlng){
console.log(latlng); //working
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("?");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
function onMapClick(e) {//Coordinate pop up
popup.setLatLng(e.latlng)
.setContent("Coordinates clicked are: " + e.latlng.toString())
.openOn(map);
var centerPoint = map.getSize().divideBy(2),
targetPoint = centerPoint.subtract([1500, 0]),
targetLatLng = map.containerPointToLatLng(centerPoint), //retrieve new lat longs
loadLatlng = "?latlng="+targetLatLng.lat+","+targetLatLng.lng;
setTimeout(function(){
//And herein lies the problem - the URL is being concatenated ??
window.location.href = window.location.href + loadLatlng;
window.location.href.reload(1);
}, 5000);
}
有谁知道如何清除然后附加网址? 感谢。
答案 0 :(得分:3)
使用部件构建网址
window.location.href = [location.protocol, '//', location.host, location.pathname, loadLatlng].join("");
与
相同window.location.href = location.protocol + '//' + location.host + location.pathname + loadLatlng;
你也可以通过拆分?
window.location.href = window.location.href.split("?")[0] + loadLatlng;