Javascript 使用jQuery each()创建谷歌地图时,背景为灰色?

Javascript 使用jQuery each()创建谷歌地图时,背景为灰色?,javascript,jquery,google-maps,Javascript,Jquery,Google Maps,我试图简化将谷歌地图添加到页面的方式 我正在为每个地图使用此标记: <div class="map" data-coordinates="-34.397, 150.644"> <div class="canvas" id="map_canvas"></div> </div> 这是谷歌地图网站上的一个游戏 当我像教程那样做时,效果很好,但是当我使用上面的代码时,我得到了灰色背景的贴图控件,我还尝试了jQuery(window.load()具

我试图简化将谷歌地图添加到页面的方式

我正在为每个地图使用此标记:

<div class="map" data-coordinates="-34.397, 150.644">
    <div class="canvas" id="map_canvas"></div>
</div>
这是谷歌地图网站上的一个游戏

当我像教程那样做时,效果很好,但是当我使用上面的代码时,我得到了灰色背景的贴图控件,我还尝试了
jQuery(window.load()具有相同的结果,问题似乎来自each(),因为当我创建没有它的映射时,它工作正常

这是有效的代码:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=SET_TO_TRUE_OR_FALSE">
    </script>
    <script type="text/javascript">
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(-34.397, 150.644),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
      }
    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>

html{高度:100%}
正文{高度:100%;边距:0;填充:0}
#地图画布{高度:100%}
函数初始化(){
变量映射选项={
中心:新google.maps.LatLng(-34.397150.644),
缩放:8,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“map_canvas”),
地图选项);
}

合作伙伴有问题

google.maps.LatLng()
接受Lat和Lng作为单独的参数,而不是复合字符串

尝试:


您可能需要测试该regexp。

在jsfiddle.net中创建一个演示程序来复制该问题。您是否为
class=“canvas”
设置了维度?@charlietfl我现在将创建一个维度,并且类画布的宽度和高度为100%,将其父级宽度和高度设置为100%,500px
coordonates
存在问题
google.maps.LatLng()
接受
Lat
Lng
作为单独的参数,而不是复合字符串。@Beetroot Beetroot您是对的,我没有注意到,请提交一个答案,这样我就可以接受它。更简单的是,您可以通过简单地将值包装在
[]
数据()中,将数组传递给
属性
方法将使用
{}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=SET_TO_TRUE_OR_FALSE">
    </script>
    <script type="text/javascript">
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(-34.397, 150.644),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
      }
    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>
jQuery(function($) {
    $('.map').each(function() {
        var $this = $(this),
            canvas = $this.find('.canvas').get(0),
            coordinates = $this.data('coordinates').split(/,\s?/);

        // set map options
        var mapOptions = {
            center: new google.maps.LatLng(Number(coordinates[0]), Number(coordinates[1])),
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        // create the map
        var map = new google.maps.Map(canvas, mapOptions);
    });
});