IE对象期望映射API v3 onload外部.js函数

问题描述 投票:0回答:2

我有一个很小的Google Maps 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> 
javascript html google-maps-api-3 dom-events onload
2个回答
2
投票

指向您地图的链接将使我们知道问题出在哪里(即使使用IE的调试器也是如此。

猜测。 IE有助于在数组的末尾添加一个附加的空对象,并在末尾添加一个“挂逗号”(后面没有逗号的逗号)的匿名对象。就像你在这里:


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.