【发布时间】:2013-07-25 16:51:18
【问题描述】:
当使用 Backbone.js 初始化 Leaflet 地图时,我无法再访问这个确切的地图了。
例如:
mapView.map.locate({setView: true, maxZoom: 10});
将导致
TypeError: 'undefined' 不是函数(评估 'mapView.map.locate({setView: true, maxZoom: 10})')
地图是通过仪表板视图中的单独视图初始化的,例如:
this.mapView = new MapView();
$(this.$el).find('.content').append(this.mapView.el).find('.map').addClass('full').css({
'height': $(window).height() / 2
});
这个视图看起来像:
var MapView = Backbone.View.extend({
template: _.template(""),
render: function ()
{
this.$el.html(this.template());
this.map = L.map(this.el).setView([48.00786, 13.17989], 8);
this.map = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'OpenStreetMap © Mitwirkende der <a href="http://osm.org/copyright">OpenStreetMap</a>'
}).addTo(this.map);
return this;
}
});
我可以通过控制台访问地图对象。记录它。结果如下:
_动画:真 _bgBuffer:HTMLDivElement _clearBgBufferTimer:4 _container: HTMLDivElement _initHooksCalled: 真 _leaflet_events:对象 _传单_id:20 _limitedUpdate: function s() {var a=arguments;return n?(o=!0,void 0):(n=!0,setTimeout(function(){n=!1,o&&(s.apply(i, a),o=!1)},e),t.apply(i,a),void 0);} _map:对象 _tileContainer: HTMLDivElement _tileImg:HTMLImageElement _tiles:对象 _tilesToLoad:0 _url: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" 选项:对象 原型:对象
但为什么我之后无法访问此地图?
谢谢!
【问题讨论】:
-
mapView.map.locate会建议您在一个视图中只有一个mapView属性时会找到一个mapView变量。并且不要$(this.$el),只需使用this.$el;this.$与this.$el.find相同。 -
谢谢 ;) 我明白你的意思,但是 mapView 和 mapView 是两个(好吧,名字不好,但无论如何)变量。 MapView 本身有一个属性 valled mapView,在路由器中(我需要渲染地图视图) var mapView 被初始化为 var mapView = view.mapView.render(); - 对不起...
标签: backbone.js leaflet