Google Maps API - 地图未显示 - 没有错误

Foo*_*Bar 2 javascript jquery google-maps google-maps-api-3

我正在尝试将Google API中的地图加载到div中.但是,未加载地图,并且不输出错误.这是代码:

// google maps

var geocoder, map;
function codeAddress(address) {
    geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var myOptions = {
        zoom: 8,
        center: results[0].geometry.location,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      }
    });
  }


  google.maps.event.addDomListener(window, 'load', function() {
    codeAddress('" . $location['address'] . " " . $location['zip'] . " " . $location['city'] . " " . $countries[$location['country']] . "');
  });
Run Code Online (Sandbox Code Playgroud)

我已经在很长一段时间内处理这个问题,而且我的想法不合时宜.

你们中的任何人都熟悉google maps api,谁知道我的代码有什么问题?我的div确实存在ID:map_canvas.

Foo*_*Bar 5

问题是:

  • 我没有使用API​​密钥(即使它不认为这是nessecary,但它工作)
  • 我没有设置div(.map_canvas {width: 500px; height: 500px;})的宽度/高度.

  • 我忘了像这样初恋:

    initialize();
    
    function initialize() {
      google.maps.event.addDomListener(window, 'load', function() {
        codeAddress('London');
      });
    }
    
    Run Code Online (Sandbox Code Playgroud)

它现在有效.谢谢!