从外部锚标签在gmap3中平移和打开infowindow

时间:2011-12-23 17:39:12

标签: jquery google-maps-api-3

我需要将地图居中并从地图外部的链接打开信息弹出窗口。该链接甚至可以是onclick =“javascript();”适用于锚。

我的地图代码是:

        $('#wtb-map').gmap3(
      { action:'init',
        options:{
          streetViewControl: false,
            mapTypeControl: false       
          //zoom: 5
        }   
      },
      { action: 'addMarkers',
        markers:[

          {lat:49.8620722, lng:6.352047, data:'<div class="infowindow"><p><strong>cccc</strong><br/> 275 Croydon Road, Beckenham<br/>Kent, BR3 3PS<br/>United Kingdom <br/>0208-6501300 <br/> <a href="">www.mywebsite.co.uk</a><br/><strong class="tipo">RESELLER</strong></p><a class="moreinfo" href="wtb-about.php">more info</a></div>'},
          {lat:46.59433,lng:0.342236, data:'<div class="infowindow"><p><strong>aaaaa</strong><br/> 275 Croydon Road, Beckenham<br/>Kent, BR3 3PS<br/>United Kingdom <br/>0208-6501300 <br/> <a href="">www.mywebsite.co.uk</a><br/><strong class="tipo">RESELLER</strong></p><a class="moreinfo" href="wtb-about.php">more info</a></div>'},
          {lat:42.704931, lng:2.894697, data:'<div class="infowindow"><p><strong>bbbbb</strong><br/> 275 Croydon Road, Beckenham<br/>Kent, BR3 3PS<br/>United Kingdom <br/>0208-6501300 <br/> <a href="">www.mywebsite.co.uk</a><br/><strong class="tipo">RESELLER</strong></p><a class="moreinfo" href="wtb-about.php">more info</a></div>'}
        ],
        marker:{
          options:{
            draggable: false,
            icon:"../img/marker.png"
          },

          events:{
             click: function(marker, event, data){
                  var map = $(this).gmap3('get'),
                  infowindow = $(this).gmap3({action:'get', name:'infowindow'});
                    if (infowindow){
                    infowindow.open(map, marker);
                    infowindow.setContent(data);
                    } else {
                    $(this).gmap3({action:'addinfowindow', anchor:marker, options:{content: data}});
                    }




             }
          }
        }
      },
      "autofit");

所以

<a href="#" onclick="code to center/open info map to the first marker">One</a>
<a href="#" onclick="code to center/open info map to the secondmarker">Two</a>
<a href="#" onclick="code to center/open info map to the thirdmarker">3</a>

欢迎任何建议。

2 个答案:

答案 0 :(得分:3)

这种方式适合我

var data = [
    {latLng:[48.8620722,2.352047], data:"Paris" ,tag:1 },
    {latLng:[46.59433,0.342236], data:"Poitiers : great city !" ,tag:2 },
    {latLng:[42.704931,2.894697], data:"Perpignan !  GO USAP !" ,tag:3 }
]
$('#map1').gmap3({
map: {
    action: 'init',
    center: [2.811371, 4.557129],
    zoom: 20,
    mapTypeId: google.maps.MapTypeId.TERRAIN,
    callback : function(){
        $.each(data, function(i, item){
            $("#list ul").append("<li><a href='#' rel='" + (i+1) + "'>" + item.data + "</a><div style='display: none;'>"+ item.data +"</div></li>");
        });
    }
},
marker: {
    values: data,
    options: {
        draggable: false
    },
    events: {
        mouseover: function(marker, event, context){
            var map = $(this).gmap3("get");
            infowindow = $(this).gmap3({get:{name:"infowindow"}});
            if (infowindow){
                infowindow.open(map, marker);
                infowindow.setContent(context.data);
            }
            else {
                $(this).gmap3({
                    infowindow:{
                        anchor:marker, 
                        options:{content: context.data}
                    }
                });
            }
        },
        mouseout: function(){
            var infowindow = $(this).gmap3({get:{name:"infowindow"}});
            if (infowindow){
                infowindow.close();
            }
        }
    }
}
});

$("#list ul li a").click(function(){
    var tagid = $(this).attr("rel");
    var data = $(this).next("div").html();
    var map = $("#map1").gmap3({get: {name:'map'}});
    var infowindow = $("#map1").gmap3({get: {name:'infowindow'}});
    var marker = $("#map1").gmap3({get: {name:"marker",tag: parseInt(tagid)}});
    if (infowindow){
        infowindow.open(map, marker);
        infowindow.setContent(data);
    } 
    else {
         $("#map1").gmap3({infowindow:{anchor:marker,options:{content: data}}});
    }
    return false;
});

答案 1 :(得分:0)

您可以使用addDomListener()来查找触发点击的ID。

文档的特定部分: http://code.google.com/apis/maps/documentation/javascript/events.html#DomEvents

google.maps.event.addDomListener(document.getElementById("foo"), 'load', initialize);

然而,基于每个项目创建一个自定义监听器我还没弄明白,我发布了相同的问题。