【问题标题】:Triggering Click On Google Map API3 Markers From A Link Elsewhere In The Page触发点击来自页面其他位置的链接的 Google Map API3 标记
【发布时间】:2012-08-14 05:31:24
【问题描述】:

我一直在阅读许多类似的主题,但看不到如何在我的代码中实现这一点......任何建议都将不胜感激。

我通过 AJAX 设置了许多标记,并根据 JSON 结果中返回的数据在我的地图下方创建了一个表格。我想在我的数据表中创建一个可点击的链接,该链接将模拟对地图上相应标记的点击,并打开已经为实际点击标记定义的信息窗口...

function display( json_results ) {

        $("#map").gmap3({action:'clear'});

        $("#map").gmap3(
            {action: 'init',
              options:{
                center:true,
                zoom:13,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: true,
                mapTypeControlOptions: {
                        style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
                },
                navigationControl: true,
                scrollwheel: true,
                streetViewControl: true
                }
            },
            {action: 'addMarkers',
                radius:100,
                markers: json_results,
                clusters:{
                            maxZoom: 10,
                            // This style will be used for clusters with more than 0 markers
                            20: {
                            content: '<div class="cluster cluster-1">CLUSTER_COUNT</div>',
                            width: 53,
                            height: 52
                            },
                            // This style will be used for clusters with more than 20 markers
                            50: {
                            content: '<div class="cluster cluster-2">CLUSTER_COUNT</div>',
                            width: 56,
                            height: 55
                            },
                            // This style will be used for clusters with more than 50 markers
                            100: {
                            content: '<div class="cluster cluster-3">CLUSTER_COUNT</div>',
                            width: 66,
                            height: 65
                            }

                    },
                marker: {
                    options: {
                        //icon: new google.maps.MarkerImage('http://maps.gstatic.com/mapfiles/icon_green.png'),
                        clickable: true
                        },
                    events:{
                        click: function(marker,event,data) {
                            $(this).gmap3({action: 'clear', name : 'infowindow'});
                            $(this).gmap3({action: 'addinfowindow', anchor: marker, options: { content:
                            '<div class="text"><strong><div style="color:navy;">' + data.itype + '</strong><br/><div id="address" snum="' + data.streetnum + '" snam="' + data.streetnam + '" styp="' + data.streettyp + '">'+ data.iaddress +'</div><br/>' + data.inum + '<br/>'+ data.datetime +'</div><hr>'+data.notes+'</div>'} })
                        },  
                        mouseover: function(marker, event, data){
                            $(this).gmap3(
                                { action:'clear', name:'overlay'},
                                { action:'addOverlay',
                                    latLng: marker.getPosition(),
                                    content:    '<div class="infobulle">' +
                                                '<div class="bg"></div>' +
                                                '<div class="text">' + data.itype +'</div>' +
                                                '</div>' +
                                                '<div class="arrow"></div>',
                                    offset: {
                                        x:-46,
                                        y:-73
                                    }
                                });
                        },
                        mouseout: function(){
                            $(this).gmap3({action:'clear', name:'overlay'});
                            }

                    } //end events
                } // end marker

                }
                ,{action:"autofit"} //end action

                );
            };

当页面被加载并且带有搜索结果的表单被提交时,我从一些 JQUERY 调用这个函数。一切都完美无缺。现在我想在地图外添加一个链接,该链接将触发对相应标记的点击...

例如:&lt;a href="javascript:google.maps.event.trigger(markers[i], "click")"&gt;See This Infowindow&lt;/a&gt; 其中 i 将是我在 JSON 中传递的值,同时我将 lat/long 和 infowindow 数据传递给上面的函数。我假设要发送用于映射的第一个数据数组是 0,第二个是 1 等等,所以我会为第一个链接设置 i=0,为第二个链接设置 i=1 等...

不确定该逻辑是否有意义,也许有更好的方法将引用传递给标记...

谁能帮我解决这个问题?也许是一个简单的函数,我可以将标记的值传递到我现有的代码中?或者任何你认为最好的方法......

感谢大师!

【问题讨论】:

  • 我认为没有更好的方法。如果您用单引号替换单击周围的双引号,您的示例应该可以正常工作。
  • 我进行了更改,但萤火虫说标记未定义...我怀疑我需要更改语法才能到达脚本中的标记,但不确定是什么?

标签: google-maps-api-3 google-maps-markers


【解决方案1】:

您是否尝试定义自己的函数来处理点击?

function myclick(i) {
    google.maps.event.trigger(markers[i],"click");
  }

 function setHTML() {
    var html = "";
    for (var i=0; i<markers.length; i++) {
        html += '<a href="javascript:myclick(' + i + ')">' + marker[i].id + '<\/a>';
    }
    document.getElementById("table").innerHTML = html;
  }

【讨论】:

  • marker[i].id 不是marker的默认属性,我是在创建时设置的。
  • 我已经弄乱了它的变体,但鉴于上面的这个例子,我应该能够简单地通过链接传递google.maps.event.trigger(markers[i],"click"),我用 I 代替 json 数组的键......它确实不工作..它只是说没有定义标记并且没有触发地图上的叠加层......我必须在这里遗漏一些明显的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 2016-01-07
  • 1970-01-01
相关资源
最近更新 更多