为什么当我指定'topleft'的位置时,我的传单控件出现在地图的左下角?

问题描述 投票:0回答:1

我创建了一个自定义控件,并将其添加到我的Leaflet地图中。即使我指定了“ topleft”的位置,为什么它也会出现在左下角?

  L.Control.Custom = L.Control.extend({
    options: {
      position: "topleft"
    },
    onAdd: function(myMap) {
      // Add reference to map
      var div = L.DomUtil.create(
        "div",
        "leaflet-bar info hit-count-control"
      );
      return div;
    },
    onRemove: function(myMap) {
      // Remove reference from map
      delete myMap.hitCountControl;
    },
});
leaflet
1个回答
1
投票

您必须使用L.setOptions(this, options);更新控制选项:

  L.Control.Custom = L.Control.extend({
    options: {
      position: "bottomleft"
    },

    initialize(options) {
        L.setOptions(this, options);
    },
    onAdd: function(myMap) {
      // Add reference to map
      var div = L.DomUtil.create(
        "div",
        "leaflet-bar info hit-count-control"
      );
      return div;
    },
    onRemove: function(myMap) {
      // Remove reference from map
      delete myMap.hitCountControl;
    },
});
new L.Control.Custom({position: 'topleft'});
© www.soinside.com 2019 - 2024. All rights reserved.