使用 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/