谷歌地图:地图没有正确加载

时间:2015-08-14 06:53:38

标签: javascript google-maps dictionary google-maps-api-3

即使是谷歌徽标,使用条款'文本,缩放按钮,标记也显示但地图未加载

我的代码

 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<script>
var geocoder;
var map;
var infowindow;
var marker;
function initialize() {
var myCoordsLenght = 6;
var defaultLat = document.getElementById('latitude').value;
var defaultLng = document.getElementById('longitude').value;
if(defaultLat =="")
    defaultLat =24;
if(defaultLng =="")
    defaultLng =78;
var latlng1;
geocoder = new google.maps.Geocoder();
var ltlg = new google.maps.LatLng(defaultLat, defaultLng);
var mapOptions = {
    center: ltlg,
    zoom: 4
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

var input = /** @type {HTMLInputElement} */(document.getElementById('pac-input'));

//var types = document.getElementById('type-selector');
//map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
//map.controls[google.maps.ControlPosition.TOP_LEFT].push(types);

var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);

infowindow = new google.maps.InfoWindow();
if(defaultLat == 24 && defaultLng ==78)
    infowindow.setContent('Click on the Your Location (or)<br> Drag Marker to the Your Location');
else
{
    geocoder.geocode({'latLng': ltlg}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
              infowindow.setContent('<div>'+results[1].formatted_address+'</div>');
              infowindow.open(map, marker);
            }
        } else
            alert("Geocoder failed due to: " + status);
    });
}
marker = new google.maps.Marker({
    position: ltlg,
    map: map,
    draggable: true,
    title: 'Click on Map/Click & Drag',
    anchorPoint: new google.maps.Point(0, -29)
});

google.maps.event.addListener(map, 'click', function(evt) {
    marker.setPosition(evt.latLng);
    geocoder.geocode({'latLng': evt.latLng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) 
                infowindow.setContent('<div>'+results[0].formatted_address+'</div>');
            else 
                infowindow.setContent('No results found');
        }
        else 
            infowindow.setContent('Geocoder failed due to: ' + status);
    });
    document.getElementById('latitude').value = evt.latLng.lat();
    document.getElementById('longitude').value = evt.latLng.lng();
});
google.maps.event.addListener(marker, 'dragend', function(evt){
    geocoder.geocode({'latLng': evt.latLng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) 
                infowindow.setContent('<div>'+results[0].formatted_address+'</div>');
            else 
                infowindow.setContent('No results found');
        }
        else 
            infowindow.setContent('Geocoder failed due to: ' + status);
    });
    document.getElementById('latitude').value = evt.latLng.lat();
    document.getElementById('longitude').value = evt.latLng.lng();
});
google.maps.event.addListener(marker, 'click', function () {
    infowindow.open(map, marker);
});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
    infowindow.close();
    marker.setVisible(false);
    var place = autocomplete.getPlace();
    if (!place.geometry)
        return;

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) 
        map.fitBounds(place.geometry.viewport);
    else {
        map.setCenter(place.geometry.location);
        map.setZoom(17);  // Why 17? Because it looks good.
    }
    marker.setPosition(place.geometry.location); //place.geometry.location.lat();
    marker.setVisible(true);
    document.getElementById('latitude').value = place.geometry.location.lat();
    document.getElementById('longitude').value = place.geometry.location.lng();

    var address = '';
    if (place.address_components) {
        address = [
            (place.address_components[0] && place.address_components[0].short_name || ''),
            (place.address_components[1] && place.address_components[1].short_name || ''),
            (place.address_components[2] && place.address_components[2].short_name || '')
        ].join(' ');
    }
    infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
    infowindow.open(map, marker);
});

  // Sets a listener on a radio button to change the filter type on Places
  // Autocomplete.
function setupClickListener(id, types) {
    var radioButton = document.getElementById(id);
    google.maps.event.addDomListener(radioButton, 'click', function() {
        autocomplete.setTypes(types);
    });
}
setupClickListener('changetype-all', []);
setupClickListener('changetype-establishment', ['establishment']);
setupClickListener('changetype-geocode', ['geocode']);
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>

HTML

<div class="controls" id="map-canvas" style="width: 650px;height:400px;"></div>

它在控制台中显示错误

Firefox =&gt; TypeError:a在main.js中为null
chrome =&gt; 未捕获的TypeError:无法读取属性&#39; addEventListener&#39;在main.js中的null

1 个答案:

答案 0 :(得分:0)

根据小提琴的html和您提供的错误信息,我的猜测是地图已正确加载,但这些行导致了您的错误

    function setupClickListener(id, types) {
        var radioButton = document.getElementById(id);
       if(radioButton){
             google.maps.event.addDomListener(radioButton, 'click', function() {
            autocomplete.setTypes(types);
        });
       }
    }

您传递的ID不在您的HTML中。

setupClickListener('changetype-all', []);
setupClickListener('changetype-establishment', ['establishment']);
setupClickListener('changetype-geocode', ['geocode']);

radioButton对象为null,因此您的错误。一个简单的if将解决问题