2013-05-06 111 views
0

JavaScript的新功能。嘗試添加標記列表(從python)到谷歌地圖。未捕獲的參考錯誤:沒有未定義

我生Jinja2的模板:

<head> 
<title>Google Map Test</title> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<style type="text/css"> 
    #map-canvas { height: 700px; width: 400px; } 
</style> 

<script type="text/javascript" 
    src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&sensor=false"> 
</script> 

<script type="text/javascript"> 
    var locations = {{ locations|safe }}; 

    /* 
    locations|safe results in the following line in my rendered template: 
    var locations = [['A.W. Hastings/Windows & Doors by Brownell', '44.4456', '-73.1276'], ['AARP', '44.4757', '-73.2113'], ['Adirondack Audiology', '44.4792', '-73.22'], ['Alchemy Jewelry Arts #1A', '44.4672', '-73.2147'], ['All Smiles Dental', '44.2334', '-72.5539']]; 
    */ 

    function initialize() { 
    var mapOptions = { 
     center: new google.maps.LatLng(43.953, -72.593), 
     zoom: 8, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map-canvas"), 
     mapOptions); 
    } 

    var infowindow = new google.maps.InfoWindow(); 

    var marker, i; 

for (var i = 0; i < locations.length; i++) { 
    marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
     map: map 
    }); 

    google.maps.event.addListener(marker, 'click', (function(marker, i) { 
     return function() { 
      infowindow.setContent(locations[i][0]); 
      infowindow.open(map, marker); 
     } 
    })(marker, i)); 
} 
</script> 

我送從我的觀點列表「位置」,因爲名單定期更換。

我正在接收未定位的參考誤差,用於定義位置變量的那一行。全部輸出是:

未捕獲的ReferenceError:無沒有定義127.0.0.1:14 (匿名功能)

+0

必須有一些代碼引用一個未聲明的標識符'None'才能生成該錯誤消息,但我在這裏沒有看到它。 – 2013-05-06 18:06:19

+0

@DaggNabbit這是從我的模板複製和粘貼。 – 2013-05-06 18:57:01

+0

*儘管渲染模板的輸出被截斷,並且在那裏找到「無」 – 2013-05-06 19:19:45

回答

1

移動下面的函數內部的功能initialize()代碼,否則變量map不可用時和你訪問它的地方。

0

我的「位置」模板標籤有超過300個項目。爲了簡單起見,我截斷了列表。這刪除了問題的根源 - 違規的「無」。

我做了Molle博士推薦的更改,然後回去刪除任何帶有「None」的列表項目作爲屬性,並且所有工作都完美無缺。

相關問題