【问题标题】:Google Maps API - Map not showing - no errorsGoogle Maps API - 地图未显示 - 没有错误
【发布时间】:2013-09-27 11:18:54
【问题描述】:

我正在尝试将 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']] . "');
  });

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

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

【问题讨论】:

  • 你能多发点吗?我认为这不足以调试代码。
  • 要么你传递到codeAddress 的地址很奇怪,要么你没有设置你的地图所在的div的宽度/高度。因为如果我只传入',你的代码就可以工作伦敦'到codeAddress

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


【解决方案1】:

问题是:

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

  • 我忘了像这样初始化:

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

现在可以了。谢谢!

【讨论】:

    【解决方案2】:

    您是否正在加载 Google Maps API...?你是不是在你的页面上错过了这样的东西?

    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=<your_api_key&sensor=false&callback=initialize"></script>
    ...
    <script>
        function initialize() {
            codeAddress('" . $location['address'] . " " . $location['zip'] . " " . $location['city'] . " " . $countries[$location['country']] . "');
        });
    </script>
    

    请看Google's example

    【讨论】:

      【解决方案3】:

      我假设您将地图加载到 'map_canvas' div 上,对吗?尝试在样式表上定义这些 div 的高度和宽度,这就是我在项目中发生的事情,它已经为我解决了......问题似乎是由于其他样式属性(如位置或浮动)而发生的。

      【讨论】:

        猜你喜欢
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-03
        • 1970-01-01
        • 1970-01-01
        • 2015-09-30
        相关资源
        最近更新 更多