2013-03-05 67 views
2

我正在使用Google Maps Javascript API ver3来显示世界位置。下面是我使用的示例代码:在IE中加载Main.js时出错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。

在此先感谢

+0

我能够连续几次重现问题,然后消失;永不回报。看起来像一个竞争条件。 – 2013-03-05 08:03:04

回答

1

我的猜测是,它是不是等到的GoogleMap的加载脚本身体的onload。理论上,身体可以比googlemap脚本加载得更快(relevant discussion)。尝试把

window.onload=initialize; 

在脚本的底部,而不是使用身体的onload,看看是否能解决您的问题。我很难再现这一点。

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

+0

对我来说,问题依然存在。 :(我试过你的建议,仍然没有成功:( – Pri 2013-03-05 18:22:04

+0

是的,我的猜测是错误的。你有办法轻易再现这个问题吗? – 2013-03-06 09:12:12

+0

嗨,似乎问题是与我的浏览器版本或别的什么,因为我在另一台机器IE8上尝试了相同的代码,并且这个问题没有出现在那里 – Pri 2013-03-07 10:43:55

相关问题