Moh*_*din 1 javascript jquery google-maps backbone.js
我使用backbone.js作为我的框架.我的问题是,当我尝试将google地图与backbone.js集成时,我遇到了这个奇怪的问题.
Uncaught TypeError: Cannot set property 'position' of undefined
Run Code Online (Sandbox Code Playgroud)
该教程来自该站点.
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;
});
Run Code Online (Sandbox Code Playgroud)
HTML
<div id="map-canvas"></div>
Run Code Online (Sandbox Code Playgroud)
谢谢.
你必须换行
var map = new google.maps.Map($('#map-canvas'), mapOptions);
Run Code Online (Sandbox Code Playgroud)
至
var map = new google.maps.Map($('#map-canvas')[0], mapOptions);
Run Code Online (Sandbox Code Playgroud)
Map()构造函数expect Node对象.另见jQuery get DOM节点?以及如何从JQuery选择器获取DOM元素
更新:如果map容器div元素的id maplist-page超过使用:
var map = new google.maps.Map($('#maplist-page')[0], mapOptions);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6832 次 |
| 最近记录: |