Leaflet 1.2 bug featureGroup 初始化问题

    最新项目在使用 Leaflet 做地图,需要增加标注,意外发现,Leaflet竟然没有良好的 label 功能,还好,Leaflet 的 Plugins 中有不少扩展。

    最后选择“Leaflet.LabelTextCollision”,demo采用的是 Leaflet version "1.0.1+ffcfcc1" 。当我把 Leaflet 换成 1.2.0 以后,

var p = L.polyline(
[ [ 35.695786, 139.749213 ],
[ 35.696786, 139.748213 ],
[ 35.695786, 139.747213 ] ], {
weight : 12,
color : '#fe57a1',
text : 'Leaflet.LabelTextCollision!!!!!!!!'
}).addTo(map);
var layers = L.featureGroup().addTo(map);
for ( var index in data) {
var d = data[index];
var latlng = L.latLng(d[0], d[1]);
var c = L.circleMarker(latlng, {
radius : 0,
text : latlng.toString()
});
layers.addLayer(c);
if (index == 3000) {
break;
}
}


其中“}).addTo(map);”和“layers.addLayer(c);”都会报错,其中一个报错截图如下

QQ截图20171128141431.png

定位问题到指定位置

QQ截图20171128141616.png


后来,我从 Leaflet 的各个版本源码页找到了 1.01 和 1.2.0 的源码,经比较,发现 1.0.1 中 canvas.js 的写法

L.Canvas = L.Renderer.extend({
    onAdd: function () {
        L.Renderer.prototype.onAdd.call(this);
        this._layers = this._layers || {};
        // Redraw vectors since canvas is cleared upon removal,
        // in case of removing the renderer itself from the map.
        this._draw();
    },


在 1.2.0 中的 canvas.js 改为

export var Canvas = Renderer.extend({
    getEvents: function () {
        var events = Renderer.prototype.getEvents.call(this);
        events.viewprereset = this._onViewPreReset;
        return events;
    },
    _onViewPreReset: function () {
        // Set a flag so that a viewprereset+moveend+viewreset only updates&redraws once
        this._postponeUpdatePaths = true;
    },
    onAdd: function () {
        Renderer.prototype.onAdd.call(this);
        // Redraw vectors since canvas is cleared upon removal,
        // in case of removing the renderer itself from the map.
        this._draw();
    },

以及 Renderer.js 的

export var Renderer = Layer.extend({
    // @section
    // @aka Renderer options
    options: {
        // @option padding: Number = 0.1
        // How much to extend the clip area around the map view (relative to its size)
        // e.g. 0.1 would be 10% of map view in each direction
        padding: 0.1
    },
    initialize: function (options) {
        Util.setOptions(this, options);
        Util.stamp(this);
        this._layers = this._layers || {};
    },

所以问题就显而易见 ,是在代码重构的时候,将 this._layers 初始化提升到了父类初始化中,然而,Javascript 的编码并不能保证初始化函数一定被调用,就比如我这次遇到的 

var layers = L.featureGroup().addTo(map);

修改方案:

在 1.2.0 的 leaflet-src.js 文件的 11710 行,插入代码

this._layers = this._layers || {};

即为

QQ截图20171128143531.png


运行结果

QQ截图20171128144104.png

至此,问题解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值