即对象预期映射api v3 onload外部.js函数

时间:2012-07-06 01:39:38

标签: javascript html5 javascript-events google-maps-api-3 onload

我有一个小的谷歌地图v3脚本,适用于所有适当的浏览器。但是当它遇到IE时,无论我使用什么事件处理程序或编写属性的不同方式,我都会得到“对象预期”错误。救命!

这里的脚本:

function include(filename)
{
    var head = document.getElementsByTagName('head')[0];

    script = document.createElement('script');
    script.src = filename;
    script.type = 'text/javascript';

    head.appendChild(script)
}


include('geoxml3.js');
include('v3_epoly.js');

  var geoXml = null;
    var map = null;
    var geocoder = null;
    var toggleState = 1;
    var infowindow = null;
    var marker = null;
    var Ploc = null;
    var directionDisplay;
    var directionsService = new google.maps.DirectionsService();
//Voting Locations: Array element id = voting pct!
    var Vloc = new Array();
        Vloc[1] = 's';
        Vloc[2] = 's';
        Vloc[3] = 's';
        Vloc[4] = 's';


 function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();  
    geocoder = new google.maps.Geocoder();
    infowindow = new google.maps.InfoWindow({size: new google.maps.Size(150,50) }); 
    var latlng = new google.maps.LatLng(32.5890, -96.308871);

    var myOptions = {
      zoom: 10,
      center: latlng,
      mapTypeControl: true,
        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
        navigationControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }


     map = new google.maps.Map(document.getElementById('map_canvas'),
        myOptions);
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directionsPanel')); 
    geoXml = new geoXML3.parser({
        map: map,
        suppressInfoWindows: true,
        polygonOptions: {clickable: false}, 
        });
    geoXml.parse('qvote.kml');
    // exml = new EGeoXml({map: map, singleInfoWindow: true, createpolygon: createPoly});
  }

function showAddress(address) {
    var contentString = '';
    Ploc = null;
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
      var point = results[0].geometry.location;
          map.setCenter(point);
          if (marker && marker.setMap) marker.setMap(null);
          marker = new google.maps.Marker({
              map: map, 
              position: point
          });
        for (var i=0; i<geoXml.docs[0].gpolygons.length; i++) {
          if (geoXml.docs[0].gpolygons[i].Contains(point)) {
            contentString = address+'<br>'+geoXml.docs[0].placemarks[i].name;
    //      contentString += '<br>'+point+'<br>polygon#'+i;
            Ploc = geoXml.docs[0].placemarks[i].name;
            i = 999; // Jump out of loop
          }
        }

        google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(contentString); 
        infowindow.open(map,marker);
        if (Ploc) calcRoute(address);
        });
    google.maps.event.trigger(marker,'click');
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
}
function calcRoute(addy) {
  var start = addy;
  var end = Vloc[parseInt(Ploc)];
  var request = {
    origin:start, 
    destination:end,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}

页面在这里:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<HTML xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script defer="defer" type="text/javascript" src="calvoteroute.js"></script>
<title>Voter Map</title> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

<style type="text/css"> 
  html { height: 100% }
  body { height: 100%; margin: 0px; padding: 0px }
.bla {
    font-family: Verdana, Geneva, sans-serif;
}
</style>
</head>
<body onLoad='initialize()'>

<div><img src="img/pollingbanner.png" width="663" height="126"/></div>
<div><form action="#" onsubmit="showAddress(this.address.value); return false;" style="height:100%; width:100%; padding:0px 0px 0px 0px; background:none;"> 
    <p>
      <label class="bla">Enter the voter's address to find location:</label> 

      <input type="text" size="40" name="address" value="3003 S. Washington, Kaufman TX 75142" class="address" /> 
    <input type="submit" value="Go Vote!"/>
    <br />
    Scroll below to see the current Polling locations
</form></div>
<div style="width: 100%; display: table;">
    <div style="display: table-row">
        <div id="map_canvas" style="width:663px; height:600px; display: table-cell;"></div>
        <div id="directionsPanel" style="width: 200px; height: 600px; display: table-cell;"></div>    
        </div>
</div>

</body> 
</html> 

2 个答案:

答案 0 :(得分:2)

指向地图的链接可让我们告诉问题所在(即使使用IE的调试器)。

猜测。 IE有助于在数组末尾添加一个额外的null对象,并在末尾添加一个“悬挂逗号”(逗号后面没有任何内容)的匿名对象。就像你在这里:

geoXml = new geoXML3.parser({
    map: map,
    suppressInfoWindows: true,
    polygonOptions: {clickable: false},   <----------------- bad in IE...
    });

这可能不是问题或唯一的问题,但它会在IE中引起问题。

答案 1 :(得分:0)

我处理了以下事件: google.setOnLoadCallback(initialize);它在IE 8/9中显示正常。