javascript google maps api v3 - 通过外部点击打开信息窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10798435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:04:11  来源:igfitidea点击:

google maps api v3 - open infowindow from an external click

javascriptjquerygoogle-mapsgoogle-maps-api-3

提问by user1051849

So i have a V3 map which is initialised like this:

所以我有一个 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
            },
        });

and a load of markers that look like this:

和一堆看起来像这样的标记:

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

and lastly i have a function to open the infowindow on he click of each maker:

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

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

My question is, what do i need to do to make a particular marker (say marker25837500) show its infowindow when a inside the page is clicked - perhaps something like:

我的问题是,当单击页面内部时,我需要做什么才能使特定标记(例如标记 25837500)显示其信息窗口 - 可能类似于:

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

I'm sure it's easy but i just can see my way though it!

我相信这很容易,但我只能看到我的方式!

Thanks

谢谢

回答by Salman

You might use triggerevent.

您可能会使用trigger事件。

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

Check this- https://developers.google.com/maps/documentation/javascript/reference#event

检查这个 - https://developers.google.com/maps/documentation/javascript/reference#event

Edit:Also noticed that you are calling onMarkerClick()when marker25837500is clicked, but you named the other function OpenInfoWindow()so you might need to change that too.

编辑:还注意到您onMarkerClick()marker25837500单击时调用,但您命名了另一个函数,OpenInfoWindow()因此您可能也需要更改它。

回答by Sean Mickey

You don't have to do anything unusual or simulate a clickon the marker; just make sure the OpenInfoWindowfunction can be reached:

您不必做任何不寻常的事情或click在标记上模拟 a ;只需确保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
});

As long as the InfoWindowis in scope (can be reached when the OpenInfoWindowfunction is called), this should work fine.

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