【问题标题】:Google Maps API v3 adding multiple markers w/ info windows w/ custom textGoogle Maps API v3 添加多个标记,带信息窗口,带自定义文本
【发布时间】:2013-05-25 09:16:27
【问题描述】:

我正在创建一个关于在挪威遇难的骑自行车者的网站。对于我的项目,我一直在使用 google maps api v3,但我对 javascript 的熟悉程度并不高。你可以在这里看到我的结果:http://salamatstudios.com/googlemapstest/

基本上我想在每个标记上都有多个带有信息窗口的标记。每个信息窗口将包含: 姓名年龄), 地点, 死亡日期, 阅读更多(将链接到网站本身的页面)。

喜欢这个例子:http://salamatstudios.com/bicycles/

我尝试只使用一个标记和信息窗口,效果很好。当我想在每个标记上添加带有自定义信息窗口的新标记时,我会卡住。目前我在第一个链接中看到的不同位置上有 3 个标记,但是当我单击标记时没有出现任何信息窗口..

如何绕过它对其进行编码以显示信息窗口?我怎样才能在每个信息窗口中都有自定义文本?完成后,我将在地图上放置大约 30-40 个标记。所有信息窗口都有不同类型的信息。

function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(65.18303, 20.47852),
      zoom: 5,
      mapTypeId: google.maps.MapTypeId.ROADMAP,


      // MAP CONTROLS (START)
      mapTypeControl: true,

      panControl: true,
      panControlOptions: {
      position: google.maps.ControlPosition.TOP_RIGHT
      },

      zoomControl: true,
      zoomControlOptions: {
      style: google.maps.ZoomControlStyle.LARGE,
      position: google.maps.ControlPosition.LEFT_TOP
      },

      streetViewControl: true,
      streetViewControlOptions: {
      position: google.maps.ControlPosition.LEFT_TOP
      },
      // MAP CONTROLS (END)



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


    // -------------- MARKER 1
    var marker1 = new google.maps.Marker({
    position: new google.maps.LatLng(59.96384, 11.04120),
    map: map,
    icon: 'img/bike5.png'
    });


    // MARKER 1'S INFO WINDOW
    var infowindow1 = new google.maps.InfoWindow({
    content: 'Name<br />Location<br />Date<br /><br /><a href="http://www.db.no" target="_blank">Read more(test link)</a>'
    });
    // End of infowindow code

    // Adding a click event to the marker
    google.maps.event.addListener(marker1, 'click', function() {
    // Calling the open method of the infoWindow
    infowindow1.open(map, marker);
    });
    // -------- END OF 1st MARKER


    // -------------- MARKER 2
    var marker2 = new google.maps.Marker({
    position: new google.maps.LatLng(60.63040, 8.56102),
    map: map,
    icon: 'img/bike5.png'
    });

    // MARKER 2'S INFO WINDOW
    var infowindow2 = new google.maps.InfoWindow({
    content: 'Name<br />Location<br />Date<br /><br /><a href="http://www.db.no" target="_blank">Read more(test link)</a>'
    });
    // End of infowindow code

    // Adding a click event to the marker
    google.maps.event.addListener(marker2, 'click', function() {
    // Calling the open method of the infoWindow
    infowindow2.open(map, marker);
    });
    // -------- END OF 2nd MARKER


    // -------------- MARKER 3
    var marker3 = new google.maps.Marker({
    position: new google.maps.LatLng(60.39126, 5.32205),
    map: map,
    icon: 'img/bike5.png'
    });

    // MARKER 3'S INFO WINDOW
    var infowindow3 = new google.maps.InfoWindow({
    content: 'Name<br />Location<br />Date<br /><br /><a href="http://www.db.no" target="_blank">Read more(test link)</a>'
    });
    // End of infowindow code

    // Adding a click event to the marker
    google.maps.event.addListener(marker3, 'click', function() {
    // Calling the open method of the infoWindow
    infowindow3.open(map, marker);
    });
    // -------- END OF 3rd MARKER


  }
  google.maps.event.addDomListener(window, 'load', initialize);

如果有人能给我一个线索,那就太好了。我试着搜索了一下,但我真的找不到我的答案。提前致谢! :-)

【问题讨论】:

    标签: javascript google-maps-api-3 marker infowindow


    【解决方案1】:

    您需要将信息窗口附加到正确的标记上。目前它们都与不存在的“标记”相关联(当您单击标记时,应该会在 javascript 控制台中显示错误消息)。

    点击监听器内部变化:

    infowindow1.open(map, marker);
    
    infowindow2.open(map, marker);
    
    infowindow3.open(map, marker);
    

    收件人:

    infowindow1.open(map, marker1);
    
    infowindow2.open(map, marker2);
    
    infowindow3.open(map, marker3);
    

    working example

    【讨论】:

    • 哇!这立即奏效了。非常感谢。还有什么方法可以在单击下一个标记并打开新的 infoWindow 时关闭最后一个 infoWindow 的功能?
    • 您的意思是像this(从Mike Williams' v2 tutorial 中的示例转换为v3)(我认为这是“v2 信息窗口行为”)?如果您希望一次只打开一个信息窗口,请为所有标记共享同一个信息窗口。如果这回答了您的问题,请accept it
    • 再次感谢!我测试了一段时间并失败了很多,但最终我让它按我想要的方式工作。感谢您的时间,geocodezip :-)
    • 以后遇到同样问题的人可以看看这个:salamatstudios.com/bicycles2
    【解决方案2】:
     google.maps.event.addListener(marker1, 'click', function() {
        // Calling the open method of the infoWindow
        infowindow1.open(map, marker);
        });
    

    改成

     google.maps.event.addListener(marker1, 'click', function() {
        // Calling the open method of the infoWindow
        infowindow1.open(map, this);
     });
    

    【讨论】:

      【解决方案3】:

      当你使用 for 循环时,除了 HoangHieu 答案之外,最好这样使用它:

      marker.info = new google.maps.InfoWindow({
        content: 'some text'
      });
      
      google.maps.event.addListener(marker, 'click', function() {
        this.info.open(map, this);
      });
      

      【讨论】:

      • 这实际上是一种更聪明的方法,而不是实例化infowindow1infowindow2等。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 2011-10-26
      • 2011-06-22
      • 1970-01-01
      • 2015-11-21
      • 2011-11-25
      • 2014-05-20
      相关资源
      最近更新 更多