javascript Google Maps API v3 添加多个带有信息窗口和自定义文本的标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16825290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:07:30  来源:igfitidea点击:

Google Maps API v3 adding multiple markers w/ info windows w/ custom text

javascriptgoogle-maps-api-3markerinfowindow

提问by Nader S

I am making a website over cyclists killed in Norway. For my project I have been using google maps api v3, but I have vague familiarity with javascript. You can see my result so far here: http://salamatstudios.com/googlemapstest/

我正在制作一个关于在挪威遇难的自行车手的网站。对于我的项目,我一直在使用 google maps api v3,但我对 javascript 有模糊的熟悉。到目前为止,您可以在此处查看我的结果:http: //salamatstudios.com/googlemapstest/

Basicly I want to have multiple markers with infowindows on each one. Each one of the infowindows will contain: Name (age), Location, Date of death, Read more (which will be linked to a page on the website itself).

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

Like this example here: http://salamatstudios.com/bicycles/

像这里的这个例子:http: //salamatstudios.com/bicycles/

I tried working with just one marker and infowindow and that worked just fine. When I want to add new markers with custom info windows on each I get stuck. At the moment I have 3 markers on different locations as seen in the first link, but none of the info windows appear when I click the marker..

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

How do I go around it to code it so the infowindows appear? And how can I have custom text in every infowindow? I am going to have about 30-40 markers on the map when it is done. All of the info windows will have different types of information.

我如何绕过它来编码它以便出现信息窗口?以及如何在每个信息窗口中使用自定义文本?完成后,我将在地图上有大约 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);

Would be great if some could give me a clue. I've tried searching around a bit, but I can't really find my answer. Thanks in advance! :-)

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

回答by geocodezip

You need to attach the infowindow to the correct markers. Currently they are all associated with "marker", which doesn't exist (and should cause an error message in the javascript console when you click on the markers).

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

Inside the click listener change:

单击侦听器内部更改:

infowindow1.open(map, marker);

infowindow2.open(map, marker);

infowindow3.open(map, marker);

To:

到:

infowindow1.open(map, marker1);

infowindow2.open(map, marker2);

infowindow3.open(map, marker3);

working example

工作示例

回答by Abhijit Gujar

In addition to HoangHieu Answer when you use for loop it better to use it this way:

除了使用 for 循环时的 HoangHieu 答案之外,最好以这种方式使用它:

marker.info = new google.maps.InfoWindow({
  content: 'some text'
});

google.maps.event.addListener(marker, 'click', function() {
  this.info.open(map, this);
});

回答by HoangHieu

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

change to

改成

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