你好,我已经在我的网页上使用javascript窗口加载谷歌地图内容。
如果我移动标记,窗口加载是活动的,但它仍然只加载1次,我想只做1次。
这是代码:
function initialize() {
var latitude =<?php echo $this->input("lat")->getData();?>;
var longitude =<?php echo $this->input("long")->getData();?>;
if(latitude == ""){
latitude = -6.2297465;
longitude = 106.829518;
}
var latlng = new google.maps.LatLng(latitude, longitude);
var map = new google.maps.Map(document.getElementById('mapAgen<?php echo $idUnik;?>'), {
center: latlng,
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: 'Set lat/lon values for this property',
icon : 'map-marker-shadow_2.png',
<?php if($this->editmode) { ?>
draggable: true
<?php }else{ ?>
draggable: false
<?php } ?>
});
google.maps.event.addListener(marker, 'dragend', function(a) {
setCookie("latitude", a.latLng.lat().toFixed(4),1);
setCookie("longitude", a.latLng.lng().toFixed(4),1);
$.ajax({
type: 'POST',
url: location.href,
async : true,
data: {
latitude : a.latLng.lat().toFixed(4),
longitude : a.latLng.lng().toFixed(4)
},
success: function(message) {
window.location.href=window.location.href;
}
});
console.log(a.latLng.lat().toFixed(4));
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
});
};
window.onload = initialize();
&#13;
答案 0 :(得分:1)
可能的解决方案:将localStorage值设置为false。然后,无论window.location.href加载了多少次,initialize()都只能有条件地运行一次,直到localStorage / cache被清除。
示例:
if (localStorage.getItem("isLoaded") === null) {
localStorage.setItem("isLoaded", "0");
}
function initialize() {
if (localStorage.getItem("isLoaded") === "0") {
localStorage.setItem("isLoaded", "1");
// Rest of the original initialize() code here.
}
}
window.onload = initialize;