GoogleMaps API v3 - 来自Infowindow链接的触发事件

时间:2013-02-10 21:46:20

标签: maps

我有一个右键单击上下文菜单的监听器,允许我执行特定于该特定信息窗口的操作。例如,这是我的代码打开并填充方向面板:

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;
                    }

                });

除了右键单击上下文菜单外,我还希望在infowindow中有一个链接执行相同的操作:

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

如何为此链接添加侦听器以执行与我的上下文菜单类似的操作?我一直在尝试使用addDomListener函数,但我不确定这是否正是我需要的。

1 个答案:

答案 0 :(得分: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();
                }
相关问题