问题 Google Maps API V3:如何从地图外部跳转到特定标记?


我有一张带有两个标记的地图。

地图的初始视图仅显示一个标记,我想在地图旁边提供一个链接,用于在单击时将地图移动到第二个标记。

这是我想要的演示,使用API​​的v2: http://arts.brighton.ac.uk/contact-university-of-brighton-faculty-of-arts (注意地图下方的链接)

这是我到目前为止所拥有的:

    <script type="text/javascript">
      function initialize() {
        var latlng = new google.maps.LatLng(50.823817, -0.135634);
        var myOptions = {
          zoom: 13,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP,  
          mapTypeControlOptions: {  
                style: google.maps.MapTypeControlStyle.DROPDOWN_MENU  
              }  ,
              scaleControl: false
          };

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

    // 1st marker  
    var marker1 = new google.maps.Marker({ position: new google.maps.LatLng(50.823817, -0.135634), map: map, title: 'my title' });
    var infowindow = new google.maps.InfoWindow({ content: 'my window content' }); 
    google.maps.event.addListener(marker1, 'click', function() { infowindow.open(map, marker1); }); 

    // 2nd marker  
    var marker2 = new google.maps.Marker({ position: new google.maps.LatLng(51.5262405, -0.074549), map: map, title: 'my 2nd title'});
    var infowindow2 = new google.maps.InfoWindow({ content: 'content for my 2nd window' }); 
    google.maps.event.addListener(marker2, 'click', function() { infowindow2.open(map, marker2); }); 
    }
    </script>

所以我想添加的是一个链接 marker2,将地图移动大约50多英里, 例如 <a href="#marker2">Second location</a>

我该怎么办?


12408
2018-05-08 02:23


起源



答案:


使用 addDomListener 将链接事件添加到将地图移动到该标记的链接(您还需要在链接标记中添加一个ID,以便在代码中引用它):

编辑:在初始化函数中设置事件:

<head>
  <script type="text/javascript"> 
    function initialize() {
      var myLatlng = new google.maps.LatLng(-34.397, 150.644);
      var myOptions = {
        zoom: 8,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      var marker2 = new google.maps.Marker({ position: new google.maps.LatLng(51.5262405, -0.074549), map: map, title: 'my 2nd title'});
      google.maps.event.addDomListener(document.getElementById("linkID"), "click", function(ev) {
        map.setCenter(marker2.getPosition());
      }
    }
  </script> 
</head> 
<body style="margin:0px; padding:0px;" onload="initialize()"> 
  <a href="javascript:function() { return false; }" id="location2">Second place</a>
  <div id="map_canvas" style="width:100%; height:100%"></div> 
</body>

14
2018-05-08 02:27





我找到了这篇关于如何获得的文章 圆形叠加作为标记 用于谷歌地图。希望能帮助到你!


2
2017-11-25 08:47