2013-02-10 16 views
0

我有一個右鍵單擊上下文菜單的偵聽器,該菜單允許我執行特定於該特定infowindow的操作。舉例來說,這裏是我的代碼打開並填補了方向面板:GoogleMaps API v3 - Infowindow中的鏈接觸發事件

google.maps.event.addListener(contextMenu, 'menu_item_selected', function(latLng, eventName){ 
        switch(eventName){ 
         case 'directions_from_click': 
          showDirections(); 
          geocoder.geocode({latLng: latLng}, function(results, status) { 
           if (status == google.maps.GeocoderStatus.OK) { 
           if (results[0]) { 
            fAddress = results[0].formatted_address; 
            $('#start').val(fAddress); 
            $('#panelWrapper').focus(); 
           } 
           } 
          }); 
          $('#panelWrapper').focus(); 
          break; 
         case 'directions_to_click': 
          showDirections(); 
          geocoder.geocode({latLng: latLng}, function(results, status) { 
            if (status == google.maps.GeocoderStatus.OK) { 
            if (results[0]) { 
             fAddress = results[0].formatted_address; 
             $('#end').val(fAddress); 
             $('#panelWrapper').focus(); 
            } 
            } 
           }); 
          $('#panelWrapper').focus(); 
          break; 
        } 

       }); 

除了右鍵單擊快捷菜單,我也想有在該信息窗口的onclick執行相同操作的鏈接:

<a class="fromLink">Directions from here</a> 

如何爲此鏈接添加一個偵聽器來執行類似於我的上下文菜單的偵聽器?我一直在嘗試addDomListener函數,但我不確定這是否正是我需要的。

回答

0

我在API中找不到任何東西,所以我最終通過一個onclick事件傳遞給LatLng函數來填充方向輸入。相當醜陋,但有點像這樣:

function addMessageMarker(marker, addlInfo){ 

    var position = String(marker.getPosition()); 
    var fromLink = '<a class="infoLink" onClick="getFrom(&quot;' + position + '&quot;)">Directions from here</a> - '; 
    ... //bunch of other stuff 
} 
function getFrom(position) { 
        showDirections(); 
        //Convert the string value into a latLng 
        var latLng = position.replace("(",""); 
        latLng = latLng.replace(")",""); 
        latLng = latLng.replace(" ",""); 
        latLngArr = latLng.split(","); 
        lat = parseFloat(latLngArr[0]); 
        lng = parseFloat(latLngArr[1]); 

        latLng = new google.maps.LatLng(lat,lng); 

        geocoder.geocode({latLng: latLng}, function(results, status) { 
         if (status == google.maps.GeocoderStatus.OK) { 
         if (results[0]) { 
         fAddress = results[0].formatted_address; 
         $('#start').val(fAddress); 

         } 
         } 
        }); 
        $('#panelWrapper').focus(); 
       } 
相關問題