谷歌地图标记infowindow href不呈现为链接

时间:2017-03-20 21:42:52

标签: google-maps

我已成功设法从mysql制作xml并在地图上显示标记(wo-hoo)。

该脚本包含一个包含一些基本数据的信息窗口。我希望用新的外部URL打开URL。

网址和广告标注显示效果非常好 - 这就是问题所在 - 我希望能够点击它:)

有人可以帮我解决这个问题吗?请记住,我用了一周时间从数据库中获取标记。我无法重建代码(我希望)。

This is the output I get

<script>
    function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      center: new google.maps.LatLng(37.4419, 10.0),
      zoom: 3
    });
    var infoWindow = new google.maps.InfoWindow;

      // Change this depending on the name of your PHP or XML file
      downloadUrl('mapxml.php', function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName('marker');
        Array.prototype.forEach.call(markers, function(markerElem) {
          var name = markerElem.getAttribute('name');
          var country = markerElem.getAttribute('country');
          var url = markerElem.getAttribute('url');
          var point = new google.maps.LatLng(
              parseFloat(markerElem.getAttribute('lat')),
              parseFloat(markerElem.getAttribute('lng')));
          var infowincontent = document.createElement('div');
          var strong = document.createElement('strong');
          strong.textContent = name
          infowincontent.appendChild(strong);
          infowincontent.appendChild(document.createElement('br'));
          var text = document.createElement('url');
          text.textContent = "<a href='" + url + "'>" + name + "</a>";
          infowincontent.appendChild(text);
          var marker = new google.maps.Marker({
            map: map,
            position: point,
          });
          marker.addListener('click', function() {
            infoWindow.setContent(infowincontent);
            infoWindow.open(map, marker);
          });
        });
      });
    }

  function downloadUrl(url, callback) {
    var request = window.ActiveXObject ?
        new ActiveXObject('Microsoft.XMLHTTP') :
        new XMLHttpRequest;

    request.onreadystatechange = function() {
      if (request.readyState == 4) {
        request.onreadystatechange = doNothing;
        callback(request, request.status);
      }
    };

    request.open('GET', url, true);
    request.send(null);
  }

  function doNothing() {}
</script>

1 个答案:

答案 0 :(得分:0)

除非您使用自定义html元素,否则以下

  var text = document.createElement('url');
  text.textContent = "<a href='" + url + "'>" + name + "</a>";

不起作用。你应该尝试改为

  var text = document.createElement('a');
  text.href=url;
  text.textContent = name;