0

我有一个小的 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> 
4

2 回答 2

2

指向您的地图的链接可以让我们知道问题出在哪里(即使使用 IE 的调试器)。

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

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

这可能不是问题或唯一的问题,但它会导致 IE 出现问题。

于 2012-07-06T05:37:53.160 回答
0

我用:处理了这个事件, google.setOnLoadCallback(initialize);它在 IE 8/9 中显示得很好。

于 2012-07-10T18:05:16.587 回答