代码之家  ›  专栏  ›  技术社区  ›  adamzwakk

谷歌地图没有出现在IE7/8中(但在IE9中有效)

  •  0
  • adamzwakk  · 技术社区  · 14 年前

    我正在使用googlemapsapi3来显示加拿大的Google地图和一堆标记,但是在IE7/8中,它根本没有显示出来,只是给了我一个灰色的矩形。它在Firefox/Chrome/Opera/IE9中加载得很好。

    代码如下:

    $(document).ready(function() {
        var browserSupportFlag = new Boolean();
        var map;
        geocoder = new google.maps.Geocoder();
        var myOptions = {
            zoom: 3,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            navigationControl: true,
            mapTypeControl: true,
            scaleControl: true,
            streetViewControl: true,
            navigationControlOptions: {
                style: google.maps.NavigationControlStyle.ZOOM_PAN
            },
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
            }
        };
        //var bounds = new GLatLngBounds(); 
        map = new google.maps.Map(document.getElementById("dealer-map"), myOptions);
    
        if (navigator.geolocation) {
            browserSupportFlag = true;
            navigator.geolocation.getCurrentPosition(function(position) {
                currentLocation = new google.maps.LatLng(position.coords.latitude, 
                                                         position.coords.longitude);
                contentString = "Location found using W3C standard";
                map.setCenter(currentLocation);
            }, function() {
            });
        }
        var geoXml = new geoXML3.parser({ map: map });
        geoXml.parse('<?php echo "/dealers.php"; ?>');
    
    });     
    

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.1&sensor=false&region=CA"></script>
    

    HTML/CSS是:

    <div class="main-content">
    <div class="wide-content">
    <h3>Find a Dealer</h3>
    <div style="width: 800px; height: 330px;" id="dealer-map"></div>
    </div>
    </div>
    

    知道怎么回事吗?

    2 回复  |  直到 11 年前
        1
  •  3
  •   Drew    14 年前

    实际上很简单,在任何情况下都需要设置中心,否则将显示一个灰色框。

    if( navigator.geolocation ) {
      ...
    } else {
      map.setCenter( new google.maps.LatLng(34,-83) );
    }
    

    试试看它现在能不能用。

        2
  •  0
  •   M.W. Felker    14 年前

    我猜是navigator.geolocation.getCurrentPosition函数中的空函数。

    navigator.geolocation.getCurrentPosition(function(position) {
            currentLocation = new google.maps.LatLng(position.coords.latitude, 
                                                     position.coords.longitude);
            contentString = "Location found using W3C standard";
            map.setCenter(currentLocation);
            /*infowindow.setContent(contentString); 
            infowindow.setPosition(currentLocation); 
            infowindow.open(map);*/
        }, function() {  // <<< empty function may fubar ie
        });
    

     navigator.geolocation.getCurrentPosition(function(position) {
        currentLocation = new google.maps.LatLng(position.coords.latitude, 
                                                     position.coords.longitude);
            contentString = "Location found using W3C standard";
            map.setCenter(currentLocation);
    
     });