首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从页面中其他位置的链接触发点击谷歌地图API3标记

从页面中其他位置的链接触发点击谷歌地图API3标记
EN

Stack Overflow用户
提问于 2012-08-14 13:31:24
回答 1查看 2.7K关注 0票数 0

我一直在阅读许多类似的帖子,看不出如何在我的代码中做到这一点……任何建议都将不胜感激。

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

代码语言:javascript
复制
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调用此函数。一切都运行得很完美。现在我想在地图之外添加一个链接,它将触发点击相应的标记……

例如:<a href="javascript:google.maps.event.trigger(markers[i], "click")">See This Infowindow</a>,其中I是一个值,在传递给上面的函数时,我会同时传递lat/long和infowindow数据。我假设要发送映射的第一个数据数组为0,第二个数组为1,依此类推,因此我将第一个链接设为i=0,第二个设为i=1,依此类推…

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

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

谢谢大师!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-14 17:44:03

你有没有试着定义一个自己的函数来处理点击?

代码语言:javascript
复制
function myclick(i) {
    google.maps.event.trigger(markers[i],"click");
  }

代码语言:javascript
复制
 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;
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11946255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档