Google地图 - 未捕获TypeError:无法设置属性' position'未定义的

时间:2014-05-20 08:33:18

标签: javascript jquery google-maps backbone.js

我使用backbone.js作为我的框架。我的问题是,当我尝试将google地图与backbone.js集成时,我遇到了这个奇怪的问题。

Uncaught TypeError: Cannot set property 'position' of undefined

本教程来自该网站。

https://developers.google.com/maps/documentation/javascript/examples/event-closure

即使我已经尝试过这些解决方案,问题仍然存在。

我的代码

JS

define(['jquery', 'underscore', 'backbone', 'text!modules/map/mapListViewTemplate.html'], function($, _, Backbone, mapListViewTemplate) {
    var MapListView = Backbone.View.extend({
        initialize: function() {},
        render: function() {
                var mapOptions = {
                    zoom: 4,
                    center: new google.maps.LatLng(-25.363882, 131.044922)
                };
                var map = new google.maps.Map($('#map-canvas'), mapOptions);
                // Add 5 markers to the map at random locations
                var southWest = new google.maps.LatLng(-31.203405, 125.244141);
                var northEast = new google.maps.LatLng(-25.363882, 131.044922);
                var bounds = new google.maps.LatLngBounds(southWest, northEast);
                map.fitBounds(bounds);
                var lngSpan = northEast.lng() - southWest.lng();
                var latSpan = northEast.lat() - southWest.lat();
                for (var i = 0; i < 5; i++) {
                    var position = new google.maps.LatLng(
                    southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random());
                    var marker = new google.maps.Marker({
                        position: position,
                        map: map
                    });
                    marker.setTitle((i + 1).toString());
                    this.attachSecretMessage(marker, i);
                }

            this.$el.html(mapListViewTemplate);
        },
        attachSecretMessage: function(marker,num) {
            var message = ['This', 'is', 'the', 'secret', 'message'];
                var infowindow = new google.maps.InfoWindow({
                    content: message[num]
                });
                google.maps.event.addListener(marker, 'click', function() {
                    infowindow.open(marker.get('map'), marker);
                });
        },
        remove: function() {
            Backbone.View.prototype.remove.call(this);
            $(window).off('scroll');
        }
    });
    return MapListView;
});

HTML

<div id="map-canvas"></div>

感谢。

2 个答案:

答案 0 :(得分:5)

你必须改变行

var map = new google.maps.Map($('#map-canvas'), mapOptions);

var map = new google.maps.Map($('#map-canvas')[0], mapOptions);

Map()构造函数expect Node object。另请参阅jQuery get DOM node?How to get a DOM Element from a JQuery Selector

更新:如果地图容器div元素的ID为maplist-page而不是使用:

var map = new google.maps.Map($('#maplist-page')[0], mapOptions);

另请参阅example at jsbin without backbone.js。

答案 1 :(得分:0)

问题是我猜测在初始化地图时还没有渲染div。所以, this.$el.html(mapListViewTemplate);应该是render方法中的第一行。

然后使用dom元素初始化Google地图,而不是像Anto Jurkovic所说的jQuery对象:var map = new google.maps.Map($('#map-canvas')[0], mapOptions);