javascript - Google Maps API - map 未显示 - 没有错误

标签 javascript jquery google-maps google-maps-api-3

我正在尝试从 Google API 将 map 加载到 div 中。但是,没有加载 map ,也没有输出任何错误。这是代码:

// 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']] . "');
  });

我已经处理这个问题很长时间了,但我没有想法。

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

最佳答案

问题是:

  • 我没有使用 API key (尽管它认为这不是必需的,但它确实有效)
  • 我没有设置我的 div 的宽度/高度 (.map_canvas {width: 500px; height: 500px;})。

  • 我忘了这样初始化:

    initialize();
    
    function initialize() {
      google.maps.event.addDomListener(window, 'load', function() {
        codeAddress('London');
      });
    }
    

现在可以了。谢谢!

关于javascript - Google Maps API - map 未显示 - 没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19049424/

相关文章:

javascript - 将对象数组转换为已排序的数组数组

javascript - 获取文本的 JavaScript 变量并将它们用作隐藏输入

javascript - 下拉菜单报错

JavaScript 多实例

jquery - 不明白为什么日期不受限制

android - Android 上的谷歌地图滞后

javascript - 如何使用 Ajax 和 session token 发出 REST API 请求?

javascript - 无法取消绑定(bind)事件监听器 - turbolinks 缓存有问题

javascript - javascript循环将GMarker添加到GMap的问题

java - Google 方向 api 跳过有关小转弯的信息