2017-10-11 33 views
-1

我确定我已经构造了这个错误,但“dragend”的事件监听器没有触发。当我将事件更改为bounds_changed或center_changed时,它只会在初始页面加载时触发。有什么突出的,为什么它是这样的行为?“dragend”事件监听器只在加载时触发(一次)Google Maps

我基本上只是想调用我的searchStores()函数,每次映射重新定位,所以它变得对用户反应。

var map; 

    $(document).ready(function() { 

     var latlng = new google.maps.LatLng(36.2994410, -82.3409052); 
     var myOptions = { 
      zoom: 12, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

     // create the new map with options 
     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 


     $("#btnSearch").click(function(){ 
      //Convert Address Into LatLng and Retrieve Address Near by 
      convertAddressToLatLng($("#txtAddress").val()); 
     }); 

     //send user to print/save view on click 
     $("#saveAs").click(function() { 
      window.location.href = "https://54.90.210.118/test.html?address=" + $("#txtAddress").val() + '&radius=' + $("#radiusO").val(); 
     }); 

     //WHAT IS GOING ON WITH THIS GUY??? 
     google.maps.event.addListener(map, "dragend", function() { 
      alert('map dragged'); 

     }); 
    }); 

    $(document).one('ready', function(){ 

     // Try HTML5 geolocation. 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function(position) { 
       var pos = { 
        lat: position.coords.latitude, 
        lng: position.coords.longitude 
       }; 
       latlngloc = new google.maps.LatLng(pos.lat, pos.lng); 
       map.setCenter(pos); 
       $("#divStores").html(''); 
       searchStores(latlngloc); 
      }, function() { 
       handleLocationError(true, infoWindow, map.getCenter()); 
      }); 
     } else { 
      // Browser doesn't support Geolocation 
      handleLocationError(false, infoWindow, map.getCenter()); 
     } 
    }); 
+0

我不太熟悉jQuery,但为什么你需要文档onReady和一个('准备')?他们不一样吗? –

+0

看不到你做错了什么。这是一个使用你的代码的jsfiddle,它工作得很好 - https://jsfiddle.net/g1s7n3tm/ –

+0

downvote?呵呵。无论如何,它仍然不是我的最终目标。我尝试过不同的浏览器等。它似乎与地理定位功能。当我将它评论出来时,听众的工作情况很好。到目前为止,我已经移除了.one(),并且已经将Geolocation移到了主document.ready()中。我仍然在玩各种选项,让他们玩得很好。我似乎无法找到重写事件列表或正在刷新地图的位置(因此清除了侦听器)。 – Lynxx

回答

0

感谢您的帮助!我发现问题出现在另一个函数上,最终清除地图对象然后重新创建它。因此删除侦听器。 对不起,我感谢您指出它在Jfiddle上的工作。我认为这与我如何称呼它有关。 我对Javascript真的很陌生,不得不一点一点地教自己。感谢所有的帮助! =)

相关问题