通过单击按钮和带有jQuery的单独JS文件,触发Google Map Marker上的单击事件

Abr*_*hin 7 html javascript jquery google-maps google-maps-api-3

我正在使用Google Map API v3和jQuery 1.11.0.

我在以下div中有一个Google Map-

<div  id="googleMap" class="map_div"></div>
Run Code Online (Sandbox Code Playgroud)

地图有4个标记,它的点击事件通过这种方式添加到JS-

google.maps.event.addListener(marker, 'click',
    (function(marker, i)                        //Adding Click Function
    {
        return function()
        {
            //Add Your Customized Click Code Here
                alert(locations[i][3]);
            //End Add Your Customized Click Code Here
        }
    })(marker, i));
Run Code Online (Sandbox Code Playgroud)

现在我在html的另一部分(外部地图)中有一个按钮,如下所示 -

<button id="3">Click Me</button>
Run Code Online (Sandbox Code Playgroud)

现在我想添加一个on click事件,它将触发索引为3的地图标记的click事件.

所以,我有像这样的HTML格式的JavaScript-

<script>
$(document).ready(function()
{
    $("#3").click(function(){
        google.maps.event.trigger(markers[3], 'click');
    });
});
</script>
Run Code Online (Sandbox Code Playgroud)

但它没有用.我认为它不能用jQuery选择标记.因为我之前选择了jQuery这样的地图 -

google.maps.event.trigger($("#googleMap")[0], 'resize');
Run Code Online (Sandbox Code Playgroud)

要重新调整地图大小.

那么,任何人都可以帮我在jQuery中使用Google Map MarkerSelector脚本.

在此先感谢您的帮助.

Hoa*_*ieu 13

这是你的意思?? 流动我的例子:我有用google.maps.event.trigger(markers[2], 'click');

它起作用了.我点击标记时出错了. 我的例子

使用Javascript

    var tam = [16.3,108];
    var toado = [[16.5,108.5,"https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQxFoh469eOsZQkuPOLpZn3R6yyIExkZCxOxf4ywfeY3v330EwP3Q"],
                [16.3,108,"https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQxFoh469eOsZQkuPOLpZn3R6yyIExkZCxOxf4ywfeY3v330EwP3Q"],
                [16,107,"https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQxFoh469eOsZQkuPOLpZn3R6yyIExkZCxOxf4ywfeY3v330EwP3Q"],
                [23,105,"https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQxFoh469eOsZQkuPOLpZn3R6yyIExkZCxOxf4ywfeY3v330EwP3Q"]]; 
    var markers = [];

    function initialize() {
    var myOptions = {   
      center: new google.maps.LatLng(tam[0], tam[1]),
      zoom: 10,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 


        for (var i = 0; i < toado.length; i++) {
            var beach = toado[i];
            urlimg =  beach[2];
            var image = new google.maps.MarkerImage(
                        urlimg,
                        null,
                        null,
                        null,
                        new google.maps.Size(15, 15));                              

            var myLatLng = new google.maps.LatLng(beach[0], beach[1]);

            markers[i] = new google.maps.Marker({
                position: myLatLng,
                map: map,
                icon: image,
                draggable : true,
                animation : google.maps.Animation.DROP
            });
            var ismove = true;
            (function(marker,i){
                google.maps.event.addListener(marker, 'click', function(){ 
                alert(toado[i][2]);
                //infowindow.open(map,this);
                }); 
            }(markers[i],i));

        }
        //setMarkers(map, beaches); 
    }
    window.onload = initialize;
Run Code Online (Sandbox Code Playgroud)

//的Html

  <body>
    <input type="button" id="btn-click" value="Click"/>
    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
  <script>
    $(document).ready(function(){
        $("#btn-click").click(function(){
            google.maps.event.trigger(markers[2], 'click');
        });
    });
  </script>
Run Code Online (Sandbox Code Playgroud)