簡體   English   中英

Mapbox標記屬性更新:為什么這不起作用?

[英]Mapbox marker property update: Why doesn't this work?

使用Mapbox JS API,我想知道為什么緩存在變量中的Marker屬性不會更新,但是它們的非緩存對應物會更新。

例如,這將按預期更新標記的自定義state屬性(在其他地方的geoJSON對象中定義):

map.markerLayer.on('click',function(e) {
  var marker = e.layer;
  var properties = marker.feature.properties;
  var id = properties.id;
  var state = properties.state;

  if (state === 'active') {
    panels.hidePanel(id, function(){
      e.layer.feature.properties['state'] = 'inactive';
    });
  } else {
    panels.showPanel(id, function(){
      e.layer.feature.properties['state'] = 'active';
    });
  }
});

但這不是:

map.markerLayer.on('click',function(e) {
  var marker = e.layer;
  var properties = marker.feature.properties;
  var id = properties.id;
  var state = properties['panel-state'];

  if (state === 'active') {
    panels.hidePanel(id, function(){
      state = 'inactive';
    });
  } else {
    panels.showPanel(id, function(){
      state = 'active';
    });
  }
});

誰能幫助我了解后者發生了什么? 為什么我不能在變量中緩存引用而不是每次更新e.layer.feature.properties['state']

這更像是一個基本的Javascript問題:對象持有對變量的引用。 如果您在對象中更改這些引用那么它們就會更新到位。 如果你自己提取變量並改變它們,那么它們就不是。 示例: http//mistakes.io/#6220549

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM