2017-10-11 35 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真的很陌生,不得不一點一點地教自己。感謝所有的幫助! =)

相關問題