触发谷歌地图标记点击 [英] trigger google maps marker click

查看:161
本文介绍了触发谷歌地图标记点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了谷歌地图来查找用户的当前位置,并将地图放在距离他们位置最近的标记处。点击标记时,打开信息框(请注意,这与infoWindow稍有不同 - http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/examples.html )。然而,我想要做的是自动打开最近的标记,而不需要用户实际点击。以下是触发标记打开的代码:

I have a google map set up to find the user's current location, and center the map at that closest marker to their location. The markers, when clicked, open up an infobox (note this is a little different than an infoWindow - http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/examples.html). What I want to do, however, is automatically open up the nearest marker without the user actually clicking. Here's the code to trigger a marker opening:

//loop through all locations to add a marker:
addMarker = function(loc) {
    var markerLoc = new google.maps.LatLng(loc.Lat, loc.Long);

    var marker = new google.maps.Marker({
        map: map,
        position: markerLoc
    });

    google.maps.event.addListener(marker, "mousedown", function() {

        var infoOpts = {
            content: loc.markerText,
            boxStyle: { background: "none transparent", width: "180px"},
            pixelOffset: new google.maps.Size(-90, 0),
            closeBoxMargin: "5px"
        };

        var ib = new InfoBox(infoOpts);
        ib.open(map, marker);
    });

    markers.push(marker);
};

所以我不得不触发相应标记的mouseDown函数,但它必须在除此之外的功能。我将在数组中引用适当的标记(markers [closestmarker])。

So somehow I have to trigger the mouseDown function of the appropriate marker, but it has to be done in a function outside of this one. I will have a reference to the appropriate marker in the array (markers[closestmarker]).

推荐答案

我发现这个问题有已经坐了很长一段时间,但是,以防万一,这个答案可能会有所帮助:
触发谷歌地图标记点击

i see that this question has been sitting for quite awhile, but, just in case, this answer may be helpful: trigger google maps marker click

祝你好运!

代码如下所示:

var marker = new google.maps.Marker({});
new google.maps.event.trigger( marker, 'click' );

这篇关于触发谷歌地图标记点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆