【问题标题】:Errors in Console when using Google Maps: Uncaught ReferenceError: icon is not defined使用 Google 地图时控制台中的错误:未捕获的 ReferenceError:未定义图标
【发布时间】:2017-08-12 23:05:51
【问题描述】:

我一直在尝试按照 Google 的教程使用 MySQL 和 PHP 实现 Google Maps。这是我的代码:

<div class="container">
  <div id="map">

  </div>
</div>
<script>
function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      center: new google.maps.LatLng(XX.XXX, XX.XXX),
      zoom: 12
    });
    var infoWindow = new google.maps.InfoWindow;

      // Change this depending on the name of your PHP or XML file
      downloadUrl('http://xxxxxx.com/xxxxxxxx.php/', function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName('marker');
        Array.prototype.forEach.call(markers, function(markerElem) {
          var id = markerElem.getAttribute('id');

          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 = id
          infowincontent.appendChild(strong);
          infowincontent.appendChild(document.createElement('br'));

          var marker = new google.maps.Marker({
            map: map,
            position: point,
            label: icon.label
          });
          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>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=XXXXXXXXXXXXXXXX&callback=initMap">
</script>

我已经成功地按照教程创建了一个用于创建 XML 数据的 PHP 脚本,我在 downloadUrl 函数中调用了该数据。但是,本教程基于使用静态 XML 文件。我认为我需要更改某些内容才能使标记出现在地图上。当我在页面上打开控制台时,出现以下错误:

Uncaught ReferenceError: icon is not defined

在 profile.php:57

在 HTMLCollection.forEach ()

在 profile.php:41

在 XMLHttpRequest.request.onreadystatechange (profile.php:77)

关于如何解决这些错误的任何想法?我已经坚持了几个小时。提前致谢!

【问题讨论】:

  • icon 变量未定义(在您开始使用的 Google 示例中)

标签: javascript php mysql google-maps maps


【解决方案1】:

您的代码没有定义icon。变化:

  var marker = new google.maps.Marker({
    map: map,
    position: point,
    label: icon.label
  });

收件人:

  var marker = new google.maps.Marker({
    map: map,
    position: point
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2013-03-08
    • 2021-04-24
    • 2019-03-31
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    相关资源
    最近更新 更多