用于在Google Maps Javascript API ver3中加载Main.js的IE出错

时间:2013-03-05 07:31:21

标签: google-maps-api-3

我正在使用Google Maps Javascript API ver3来显示世界位置。以下是我正在使用的示例代码:

<!DOCTYPE html> 
<html>
   <head>
     <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
     <style type="text/css">
       html { height: 100% }
       body { height: 100%; margin: 0; padding: 0 }
       #map_canvas { height: 100% }
     </style>
     <script type="text/javascript"       src="http://maps.googleapis.com/maps/api/js?sensor=true">
     </script>
     <script type="text/javascript">
        function addMarkers(location,locationDetail,map){

            var color = "#000000"

            if(locationDetail[1]=="A"){
                color = "#FF0000";
                scl = 3;
            }
            else if(locationDetail[1]=="B"){
                color = "#0000FF"
                scl = 4;
            }
            else if(locationDetail[1]=="C"){
                color = "#00FF00"
                scl = 5;
            }

            var marker = new google.maps.Marker({
                position: location,
                title: locationDetail[0],
                icon: { 
                    path: google.maps.SymbolPath.CIRCLE,
                    scale: scl,
                    fillColor: color,
                    fillOpacity:1,
                    strokeWeight:1
                }
            });


            // To add the marker to the map, call setMap();
            marker.setMap(map); 

    }
       function initialize() {

        //Marking Latitude and Longitude
        var myLatlng = [
                new google.maps.LatLng(24.466667,54.366667),
new google.maps.LatLng(-34.4,-58.24),
new google.maps.LatLng(-33.8641,151.0823)

                   ];
        var myLatlngDet = [
["Abu Dhabi","A"],
["Buenos Aires","B"],
["HOMEBUSH","C"]        
                  ];

        //Map Options to customize map
            var mapOptions = {
            zoom:2,
        center: new google.maps.LatLng(40,0),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapMaker: true,
        minZoom : 2,
        scrollwheel: false,
        mapTypeControl:true,
        mapTypeControlOptions: { 
            style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
            position: google.maps.ControlPosition.BOTTOM_CENTER
        },
        scaleControl:true,
        scaleControlOptions: { 
            position: google.maps.ControlPosition.TOP_LEFT
        },
        streetViewControl:true,
        streetViewControlOptions: {
                position: google.maps.ControlPosition.LEFT_TOP     
        }, 
        overviewMapControl:false,
        zoomControl:true,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.LARGE,
            position: google.maps.ControlPosition.LEFT_CENTER
        },
        panControl:true,
        panControlOptions: {
            position: google.maps.ControlPosition.TOP_RIGHT 
        }
         };

     //Generating map in the div
         var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);


    for(i=0; i < myLatlng.length; i++){

        addMarkers(myLatlng[i],myLatlngDet[i],map);
    }

       }     
      </script>
   </head>

   <body onload="initialize()">
     <div id="map_canvas" style="height: 100%; width: 80%;">
     </div>   
   </body> 
</html>

问题是 - 有时候标记会正确显示,但有时会出现如下javascript错误:

'对方法或属性访问的意外调用' main.js

你能帮我找出问题的原因吗?

我正在使用IE8。

提前致谢

1 个答案:

答案 0 :(得分:1)

我的猜测是,在加载googlemap的脚本之前,不会等待正文的onload。理论上,可以比googlemap脚本(relevant discussion)更快地加载正文。试试

window.onload=initialize;

在脚本的底部,而不是使用正文的onload,看看这是否解决了你的问题。我很难复制这个。

更新 你应该等到加载了googlemap后加载窗口加载。看看这个问题:How can I check whether Google Maps is fully loaded?