【问题标题】:Error in IE for loading Main.js in Google Maps Javascript API ver3IE 在 Google Maps Javascript API ver3 中加载 Main.js 时出错
【发布时间】:2013-03-05 07:31:21
【问题描述】:

我正在使用 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。

提前致谢

【问题讨论】:

  • 我能够连续多次重现该问题,然后它就消失了;永远不会回来。似乎是一种竞争条件。

标签: google-maps-api-3


【解决方案1】:

我的猜测是身体的 onload 并没有等到 googlemap 的脚本被加载。理论上,body 可以比 googlemap 脚本 (relevant discussion) 更快地加载。试试把

window.onload=initialize;

在脚本的底部而不是使用正文的 onload,看看这是否能解决您的问题。我很难重现这个。

更新 您应该等到 googlemap 加载完毕,然后再加载窗口。看看这个问题:How can I check whether Google Maps is fully loaded?

【讨论】:

  • 对我来说问题仍然存在。 :(我尝试了你的建议,仍然没有成功:(
  • 是的,我的猜测是错误的。你有办法轻松重现这个问题吗?
  • 您好,看来问题出在我的浏览器版本或其他问题上,因为我在另一台机器 IE8 上尝试了相同的代码,但没有出现此问题。
  • 不,这很可能是竞争条件。我在 IE10 运行 IE8 模式时也遇到了问题,但只有一两次。我认为问题出在IE8上。不同的机器可能更快或更慢,因此不会导致竞争条件。
  • @mrhobo - 我认为你对比赛条件的看法是正确的。就我而言,当我尝试使用marker.setMap(map); 将第一个标记添加到地图时,它会间歇性地发生。如果我在该呼叫周围添加setTimeout 大约一秒钟左右,它就会消失。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-26
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多