【问题标题】:How can I populate a dropdown list in an infowindow (Google Maps API v3)?如何在信息窗口(Google Maps API v3)中填充下拉列表?
【发布时间】:2012-06-12 00:35:30
【问题描述】:

我有两个数组:一个用于文章名称,另一个用于每篇文章的 url 链接。我正在尝试使用文章名称填充信息窗口中的下拉列表,并在选择时链接到每篇文章。

似乎链接可能与使用onchange 事件有关,否则我使用"domready" eventListener 来访问<select> 元素的id 标签。

这是我目前拥有的相关代码,工作:

function setDropDownList(mapMarker, names, links)
{
    // event listener for dropdown list in the map markers' infowindow
    google.maps.event.addListener(mapMarker, "domready", function()
    {
        var articles = document.getElementById("select");

        var nextArticle, nextOption;

        for(var i = 0; i < names.length; i++)
        {
            nextArticle = names[i];
            nextOption = new Option(nextArticle);

            /* add the new option to the option list
             ("null" for IE5, "-1" for all other browsers) */
            try
            {
                articles.add(nextOption, -1);
            }
            catch(e)
            {
                articles.add(nextOption, null);
            }
        }
    });
} // end of function setDropDownList

【问题讨论】:

  • ...我刚刚用addDomListener而不是addListener尝试过这个,也无济于事...

标签: javascript drop-down-menu google-maps-api-3 infowindow domready


【解决方案1】:

由于我在放置标记后调用 setDropDownList 函数,因此我删除了该侦听器并添加了一个以在单击标记时显示信息窗口。

演示 http://jsfiddle.net/yV6xv/10/

在进行选择时如何处理 onchange 事件取决于您。现在,它会通过一条消息和一个虚拟链接发出警报。

function setDropDownList(mapMarker, mapInfoWindow, names, links)
{

        var articles = document.getElementById("select");
        articles.onchange = function() {
          alert("Going to link " + links[this.options.selectedIndex]);
        };
        var nextArticle, nextOption;

        for(var i = 0; i < names.length; i++)
        {
            nextArticle = names[i];
            nextOption = new Option(nextArticle);

            /* add the new option to the option list
             ("null" for IE5, "-1" for all other browsers) */
            try
            {
                articles.add(nextOption, -1);
            }
            catch(e)
            {
                articles.add(nextOption, null);
            }
            mapInfoWindow.setContent(document.getElementById("select"));

            google.maps.event.addListener(mapMarker, 'click', function() {
              mapInfoWindow.open(map, this);
            });
    }
} // end of function setDropDownList

我应该补充一点,map 是全局的。

  var map;
  var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 2,
    mapTypeId: google.maps.MapTypeId.ROADMAP };

  function initialize() {
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    var mymarker = new google.maps.Marker({
      map:map,
      position:new google.maps.LatLng(0,0)
    });

    var mynames = ["-- Choose an article --", "How to Launch a Fireball", "The Trinity of Magic", "Defending Against the Hax"];
var mylinks = ["to current page itself #", "http://fireball", "http://trinity", "http://hax"];

    var myinfowindow = new google.maps.InfoWindow();
    setDropDownList(mymarker, myinfowindow, mynames, mylinks);
  }

【讨论】:

  • 这太棒了,完美运行,谢谢@Lilina!如果这件事能让我给你+10...所以,我对是否使用"domready" eventListener 感到困惑是因为我希望在@987654327 的content 属性中创建选项列表@(with 一个 id 标记),而不是在 html 中。 “domready”对此不起作用,所以我选择了一个不同的解决方案——只是用选项列表的一串 html 标签设置 content 属性(见下文)。
  • 你说得对,最好不要在 html 中定义 select (似乎在调用 setContent 时它会从 HTML 跳转到信息窗口)。我的一般策略是通过更改最少量的原始代码来解决问题/答案问题,所以我默默地将select 单独留在了 HTML 中。如果您可以设法在脚本中仅使用selectoptions 定义一个字符串,并将其传递给setContent,我认为这将是最好的。
【解决方案2】:

这是我的解决方案,在我的情况下,我需要在 infowindow 的 content 属性中创建整个选项列表,而不是在 html 中。

因此,我没有引用 html 选择元素的 id,而是简单地将选项列表连接为字符串,循环添加每个选项(我仍然不理解 "domready" eventListener,因为它不适用于这种方法)。

我没有使用@Lilina 优雅的.onchange = function(){... 作为链接,而是使用window.location.href = this.options[this.selectedIndex].value,将每个选项的值属性设置为等于其'url 链接。

这是代码:

function setDropDownList(mapRef, mapMarker, markerInfoWindow, names, links)
{
    var articles = markerInfoWindow.content;
    articles += "<select onchange = \"window.location.href = this.options[this.selectedIndex].value;\">";
    articles += "<option>&nbsp;&mdash;&nbsp;select an article&nbsp;&mdash;&nbsp;</option>";



    var nextArticle, nextArticleLink;

    for(var i = 0; i < names.length; i++)
    {
        nextArticle = names[i];
        nextArticleLink = links[i];

        articles += "<option value = " + nextArticleLink + ">" + nextArticle + "</option>";

        // setting each info window for each map marker with the function below
        setInfoWindow(mapRef, mapMarker, markerInfoWindow);
    }



    articles += "</select>";
    markerInfoWindow.setContent(articles);
} // end of function setDropDownList

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    相关资源
    最近更新 更多