将RequireJS与Google地图一起使用

时间:2013-08-10 13:56:13

标签: javascript google-maps requirejs

我尝试连接RequireJS和谷歌地图。我有这个代码

    require(['gmapapi'], function(google){
    var map;

      var mapOptions = {
        zoom: 8,
        center: new google.LatLng(-34.397, 150.644),
        mapTypeId: google.MapTypeId.ROADMAP
      };
      map = new google.Map(document.getElementById('map'),
          {});

});

在require config gmapapi: '//maps.googleapis.com/maps/api/js?sensor=false',

但我收到错误Cannot read property 'LatLng' of undefined如何访问此对象?

1 个答案:

答案 0 :(得分:0)

我使用异步插件解决了这个问题。在路径中添加

async:'//raw.github.com/millermedeiros/requirejs-plugins/master/src/async',

在JavaScript文件中:

require(['async!//maps.googleapis.com/maps/api/js?sensor=true'], function(){
var mapDiv = document.getElementById('map');

      var map = new google.maps.Map(mapDiv, {
        center: new google.maps.LatLng(37.4419, -122.1419),
        zoom: 13,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        navigationControl: true,
        navigationControlOptions: {
          style: google.maps.NavigationControlStyle.SMALL
        }
});