javascript - Mapbox标记属性更新: Why doesn't this work?

标签 javascript json mapbox

使用 Mapbox JS API 并想知道为什么缓存在变量中的标记属性不会更新,但它们的非缓存对应项却会更新。

例如,这将按预期更新标记的自定义 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

关于javascript - Mapbox标记属性更新: Why doesn't this work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18150587/

相关文章:

javascript - 如何在点击时删除特定的李列表?

javascript - Vue.js 输入框在输入一个字符后失去焦点

javascript - Rails- Bootstrap 设置

Javascript递归导致循环结构

mapbox - 我可以像在 mapbox.js 中一样在 Mapbox-gl.js 中创建分屏吗?

leaflet - 打印 map 盒/单张 map

javascript - 使用 d3.csv 读取键值对

java - 为什么 Google JSON 库无法正确序列化此 HashMap?

javascript - 来自 json 文件的数据,用于 CHAPS 时间线

mapbox - 如何禁用符号淡出、淡入效果?