我只是在第一次点击时尝试执行下面的点击(功能),但每当我点击地图时它总会执行。 是否可以使用以下代码单击?提前致谢
var placedMarkers = 0;
var availableMarkersToPlace = 1;
setTimeout( function(){
if(placedMarkers >= availableMarkersToPlace)
return;
placedMarkers++;
var map = Appery("google_map").gmap;
google.maps.event.addListener(map, 'click', function(event) {
localStorage.setItem('selectedLat', event.latLng.lat());
localStorage.setItem('selectedLng', event.latLng.lng());
placeMarker(event.latLng,map);
alert(event.latLng);
});
}, 1000);
答案 0 :(得分:1)
使用addListenerOnce
google.maps.event.addListenerOnce(map, 'click', function(event) {
localStorage.setItem('selectedLat', event.latLng.lat());
localStorage.setItem('selectedLng', event.latLng.lng());
placeMarker(event.latLng,map);
alert(event.latLng);
});
答案 1 :(得分:0)
只需创建一个标志值,它将决定,运行你的函数或跳过:
var placedMarkers = 0;
var availableMarkersToPlace = 1;
var clicked = false;
setTimeout( function(){
if(placedMarkers >= availableMarkersToPlace)
return;
placedMarkers++;
var map = Appery("google_map").gmap;
google.maps.event.addListener(map, 'click', function(event) {
if(!clicked) {
clicked = true;
localStorage.setItem('selectedLat', event.latLng.lat());
localStorage.setItem('selectedLng', event.latLng.lng());
placeMarker(event.latLng,map);
alert(event.latLng);
}
});
}, 1000);