【问题标题】:Changing the Google Maps Marker HTML after a function is ran运行函数后更改 Google 地图标记 HTML
【发布时间】:2012-11-22 01:07:45
【问题描述】:

因此,我试图找出一种方法来更改 Google Maps V3 标记的 HTML,在它们从数据库中提取之后但在它们被推送到数组之前。

当调用 getFishing() 时,我想运行 convertRate(rate) 以便如果 rate 变量等于或大于 2,它会显示标记本身的 HTML 中的图片。我已经尝试将它放在 bindInfoWindow4() 中,并且在 getFishing() 函数中尝试了几个地方,但没有成功。有没有人这样做过?标记被推到fishArray之后是否可以?

     function getFishing() {
        fishingUrl("XML_Fishing.php", function (data) {
            var xml = data.responseXML;
            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
                var id = markers[i].getAttribute("id");
                var title = markers[i].getAttribute("title");
                var rate = markers[i].getAttribute("rate");
                var Fishhtml = "<img id='1star' src='images/1star.png' style='visibility:hidden'>";
                var icon = FishingIcon;
                var Fishmark = new google.maps.Marker({
                    map: map,
                    position: point,
                    icon: icon.icon
                });
                fishArray.push(Fishmark);
                bindInfoWindow4(Fishmark, map, Fishinfo, Fishhtml);

            }
        });
    }
    function convertRate(rate) {
        if (rate >= 2) {
            document.getElementById("1star").style.visibility = 'visible';
        }
    }

    function bindInfoWindow4(marker, map, infoWindow, html) {
        google.maps.event.addListener(marker, 'click', function () {
            infoWindow.setContent(html);
            infoWindow.open(map, marker);
        });
    }

【问题讨论】:

    标签: javascript google-maps-api-3


    【解决方案1】:

    如果您更改您的点击侦听器以显示保存在标记的成员变量中的 HTML,您可以随时更改它。如果 InfoWindow 已打开,您可能希望将其关闭并重新打开(或更新其内容,但这会变得更加复杂)。

    类似:

      function bindInfoWindow4(marker, map, infoWindow, html) {
          marker.myHtmlContent = html;
          google.maps.event.addListener(marker, 'click', function() {
            infoWindow.setContent(marker.myHtmlContent);
            infoWindow.open(map, marker);
          });
      }
    

    然后通过更改 marker.myHtmlContent 中的值来更新内容。为了让它可见,像这样:

      marker.myHtmlContent = "<img id='1star' src='images/1star.png' style='visibility:visible'>";
    

    【讨论】:

    • 你能帮我解决我将如何指定更改 marker.myHtmlContent 的内容吗?我所知道的就是通过指定元素 ID 的“id”来更改内容。您不必给我确切的答案,但请让我滚动。
    • 会不会是这样的:marker.myHtmlContent.getElementById("1star").style.visibility='visible';
    • 这可行,但我实际上取出了一堆不相关的 HTML,这些 HTML 也与 infoWindow 一起使用(它不仅仅是一张图片)。有没有办法只更新 infoWindow 的一张图片的可见性,或者我只需要存储两个完整版本的 HTML?
    • 我用 marker.myHtmlContent = html + "";
    • 再次感谢,这不是你第一次帮助我,我真的很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 2013-01-14
    • 1970-01-01
    • 2019-09-18
    • 2017-07-28
    • 2017-10-19
    • 1970-01-01
    相关资源
    最近更新 更多