Google Map-未捕获的TypeError:无法设置未定义的属性"position" [英] Google map - Uncaught TypeError: Cannot set property 'position' of undefined
问题描述
我正在使用bone.js作为我的框架.我的问题是,当我尝试将google map与ribs.js集成时遇到了这个奇怪的问题.
I'm using backbone.js as my framework. My problem is, I got this weird problem when I try integrating google map with backbone.js.
Uncaught TypeError: Cannot set property 'position' of undefined
该教程来自此站点.
https://developers.google.com/maps/documentation /javascript/examples/event-closure
即使我已经尝试了这些解决方案,问题仍然存在.
Even I already try these solutions, the problem still occurring.
- Uncaught TypeError: Cannot set property 'position' of undefined
- Google maps request returning "Cannot set property 'position' of undefined"
我的下面的代码
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>
谢谢.
推荐答案
您必须更改行
var map = new google.maps.Map($('#map-canvas'), mapOptions);
到
var map = new google.maps.Map($('#map-canvas')[0], mapOptions);
Map()构造函数期望Node
对象.另请参见 jQuery获取DOM节点?和
Map() constructor expect Node
object. See also jQuery get DOM node? and How to get a DOM Element from a JQuery Selector
更新:如果地图容器div元素的ID为maplist-page
而不是使用:
Update: If id of map container div element is maplist-page
than use:
var map = new google.maps.Map($('#maplist-page')[0], mapOptions);
See also example at jsbin without backbone.js.
这篇关于Google Map-未捕获的TypeError:无法设置未定义的属性"position"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!