“未捕获的引用错误:未定义初始化”

时间:2013-10-15 21:29:14

标签: google-maps google-maps-api-3

谷歌地图初始化仍然存在问题。 我也看不到插入的所有标记。 我声明我不懂javascript,我发现很难理解我错在哪里。 我查看了几个帖子,我自己的问题,但我没有成功,找到我的问题的解决方案。

这是我的文件googlemap.js

var markers = [
['<b style="color:red;"></b></br> Main Office - Malta', 35.895079, 14.49011], 
['<b style="color:red;"></br> Desk Office - Principaute de Monaco', 43.75042,7.438238],
['<b style="color:red;"></b></br> Desk Office - Belgium', 50.50389, 4.46994],
['<b style="color:red;"></b></br> Desk Office - Cyprus', 35.038174, 33.40443],
['<b style="color:red;"></b></br> Desk Office - Hong-Kong', 22.354694, 114.131719],
['<b style="color:red;"></b></br> Desk Office - Jersey', 40.727865, -74.078228],
['<b style="color:red;"></b></br> Desk Office -  The Netherlands', 52.106505,5.515137],
['<b style="color:red;"></b></br> Desk Office - Russia & CIS', 55.719648, 37.567421],
['<b style="color:red;"></b></br> Desk Office - United Kingdom', 52.509535,-0.791016],
['<b style="color:red;"></b></br> Desk Office - China', 34.412677, 103.954634],
['<b style="color:red;"></b></br> Desk Office -  Guernsey', 40.725812,-73.954167],
['<b style="color:red;"></b></br> Desk Office - Ireland', 40.737158, -73.886638],
['<b style="color:red;"></b></br> Desk Office -  Luxembourg', 49.614561,6.132495],
['<b style="color:red;"></b></br> Desk Office -  Mauritius', -20.286556, 57.504439],
['<b style="color:red;"></b></br> Desk Office -  New-York', 40.729379, -74.003781],
['<b style="color:red;"></b></br> Desk Office - Singapore', 1.283094,103.8439]

     ];

function initializeMaps() {
var latlng = new google.maps.LatLng(35.895079, 14.49011);
var myOptions = {
    zoom: 15,
    center: latlng,
    navigationControl: false,
navigationControlOptions: {style: google.maps.NavigationControlStyle.NORMAL},
streetViewControl: false,
//
zoomControl: true,
zoomControlOptions: {
  style: google.maps.ZoomControlStyle.SMALL,
  position: google.maps.ControlPosition.TOP_LEFT
},
  mapTypeControl: true,
 navigationControl: false,
mapTypeControlOptions: {

 position: google.maps.ControlPosition.TOP_RIGHT, style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
mapTypeIds: [google.maps.MapTypeId.TERRAIN, google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, "blackMap"]

},


};    


var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);

//Bl map style


 var styleBl =
[


{
"featureType": "water",
"stylers": [
  { "saturation": -21 },
  { "hue": "#00e5ff" },
  { "lightness": 21 }
]


 },{
"featureType": "landscape",
"stylers": [
  { "hue": "#1f78c4" }
]


 },{
"featureType": "road",
"elementType": "labels.text",
"stylers": [
  { "visibility": "on" },
  { "color": "#354a82" }
]

 },{
 },{
"featureType": "poi.business"  }
]

 var styledMapOptions = {name: "Bl"}


 var blMapType = new google.maps.StyledMapType(styleBwl, styledMapOptions);


 map.mapTypes.set("blackMap", blMapType);

map.setMapTypeId("blackMap");
 var infowindow = new google.maps.InfoWindow(); 

var marker, i;
     var bounds = new google.maps.LatLngBounds();


for (i = 0; i < markers.length; i++) { 
     var pos = new google.maps.LatLng(markers[i][1], markers[i][2]);
     bounds.extend(pos);
     marker = new google.maps.Marker({
     position: pos,
     map: map
         });
     google.maps.event.addListener(marker, 'click', (function(marker, i) {
     return function() {
     infowindow.setContent(markers[i][0]);
     infowindow.open(map, marker);
     }
     })(marker, i));
     }
     map.fitBounds(bounds);
     }
google.maps.event.addDomListener(window, 'load', initialize);

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

任何形式的帮助都表示赞赏。

感谢

1 个答案:

答案 0 :(得分:3)

这是因为您要向不存在的函数(initialize)添加事件侦听器。您的功能称为initializeMaps,因此将google.maps.event.addDomListener(window, 'load', initialize);更改为google.maps.event.addDomListener(window, 'load', initializeMaps);