2017-08-12 37 views
-1

我一直在試圖遵循Google的教程來實現帶有MySQL和PHP的Google Maps。這裏是我的代碼:使用Google地圖時在控制檯中的錯誤:未捕獲的ReferenceError:圖標未定義

<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文件。我認爲我需要改變一些東西,讓標記出現在地圖上。當我打開控制檯我的網頁上,即時得到以下錯誤:

未捕獲的ReferenceError:57

在HTMLCollection.forEach()

:圖標沒有在profile.php定義

在profile.php:41

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

關於如何解決噸任何想法這些錯誤?我一直堅持這幾個小時。提前致謝!

+0

的'icon'變量沒有定義(而它在您開始使用谷歌的例子) – geocodezip

回答

0

您的代碼未定義icon。變化:

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

要:

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