2016-09-25 70 views
2

我正在尝试为Leaflet地图查看器开发插件。我有一个带有选项属性列表的类,但是当我尝试访问它们时,使用这个关键字的方法不起作用onAdd如何从使用此关键字的方法访问类propertys

L.btsControl = L.Control.extend({ 
options: { 
    position: 'topright', 
    //control position - allowed: 'topleft', 'topright', 
    //'bottomleft', 'bottomright' 
    minValue: 'isss', 
    min: 0, 
    max: 1, 
    maxValue: '', 
}, 

initialize: function (options) { 
    L.Util.setOptions(this, options); 
}, 

onAdd: function (map) { 
    this._map = map; 
    this._map.eachLayer(function (layer){ 
     if (layer.options.time_i > this.options.max) { 
      this.options.maxValue = layer.options.time; 
     } 
     if (layer.options.time_i < this.options.min) { 
      this.options.minValue = layer.options.time; 
     } 
    }); 

//SKIPED CODE 

    return container; 
}, 

那么,如何从onAdd方法访问最小和最大属性?现在我越来越

"TypeError: this.options is undefined"

调用函数是:var mycontrol = new L.btsControl().addTo(map);

+0

我编辑问题 – Bungow

+0

请拼写检查您的文章和标题。 – 2016-09-25 13:53:33

回答

1

您提供.eachLayer()回调函数没有你想要的内容。您可以通过该功能结合到你的this解决此问题:

this._map.eachLayer(function (layer){ 
    if (layer.options.time_i > this.options.max) { 
     this.options.maxValue = layer.options.time; 
    } 
    if (layer.options.time_i < this.options.min) { 
     this.options.minValue = layer.options.time; 
    } 
}.bind(this)); 

How does the this keyword work