google maps api v3 - 从外部点击打开infowindow

时间:2012-05-29 11:42:08

标签: javascript jquery google-maps google-maps-api-3

所以我有一张初始化的V3地图:

function init() {
        var mapCenter = new google.maps.LatLng(51.5081289,-0.128005);
        var map = new google.maps.Map(document.getElementById('map'), {
          'zoom': 6,
          'center': mapCenter,
          'mapTypeId': google.maps.MapTypeId.ROADMAP, 
          panControl: false,
          mapTypeControl: false,
          zoomControl: true,
          zoomControlOptions: {
                style: google.maps.ZoomControlStyle.SMALL,
                position: google.maps.ControlPosition.LEFT_TOP
            },
        });

以及大量标记符如下:

var marker25837500 = new google.maps.Marker({
              map: map, 
              pop_title: 'blah blah',                                                                                                 
              pop_wind: 'more blah',
                      zIndex: 999,
              icon: 'images/map_icons/s6.png'
            }); 
            google.maps.event.addListener(marker25837500, 'click', onMarkerClick);

最后我有一个功能,在点击每个制造商时打开信息窗口:

var infoWindow = new google.maps.InfoWindow;

var onMarkerClick = OpenInfoWindow;

function OpenInfoWindow() {
          var marker = this;
          infoWindow.setContent('<h3>' + marker.pop_title + '</h3>' +
                                         marker.pop_body);

          infoWindow.open(map, marker);
        };
        google.maps.event.addListener(map, 'click', function() {
          infoWindow.close();
        }); 

我的问题是,当点击页面内部时,我需要做些什么来制作一个特定的标记(比如marker25837500)显示它的信息 - 可能是这样的:

<div id="marker25837500">click to see infoWindow!</div>

我确信这很容易,但我只能通过它看到我的方式!

由于

2 个答案:

答案 0 :(得分:11)

您可以使用trigger事件。

$('#marker25837500').click(function () {
    google.maps.event.trigger(marker25837500, 'click')
})

选中此项 - https://developers.google.com/maps/documentation/javascript/reference#event

修改:还注意到您在点击onMarkerClick()时正在调用marker25837500,但您将其他功能命名为OpenInfoWindow(),因此您可能需要更改该功能太

答案 1 :(得分:2)

您无需在标记上执行任何异常操作或模拟click;只需确保可以访问OpenInfoWindow功能:

//This is the simple case:
var myDiv = document.getElementById( "marker25837500" );
google.maps.event.addDomListener( myDiv, "click", OpenInfoWindow );

//Or if you have other things that you want to accomplish when this occurs:
var myDiv = document.getElementById( "marker25837500" );
google.maps.event.addDomListener( myDiv, "click", function() {
    OpenInfoWindow();
    //Do other stuff here
});

只要InfoWindow在范围内(可以在调用OpenInfoWindow函数时到达),这应该可以正常工作。