【问题标题】:Can't access Leaflet map with Backbone.js无法使用 Backbone.js 访问 Leaflet 地图
【发布时间】: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 &copy; 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.$elthis.$this.$el.find 相同。
  • 谢谢 ;) 我明白你的意思,但是 mapView 和 mapView 是两个(好吧,名字不好,但无论如何)变量。 MapView 本身有一个属性 valled mapView,在路由器中(我需要渲染地图视图) var mapView 被初始化为 var mapView = view.mapView.render(); - 对不起...

标签: backbone.js leaflet


【解决方案1】:

问题解决了。我只是将整个地图的东西移到了视图中,并没有将地图分离到另一个视图中。这可能不是最好的方法,但它做得很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-07
    • 2020-08-20
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    相关资源
    最近更新 更多