MapBoxGL Draw:为什么在我看到值时事件的“ FeatureTarget”未定义?

时间:2020-06-15 05:26:40

标签: javascript mapbox-gl-js mapbox-gl-draw

我要做的是访问从MapboxGL返回的鼠标事件对象的featureTarget属性。

我的代码是这样的:

// map is the instance of MapboxGL
map.on('mousemove', e => {
    console.log("***");
    console.log(e);
    console.log(e.featureTarget);
    console.log("***");
});

然后Chrome的控制台显示如下:

***
 n {point: c, lngLat: B, originalEvent: MouseEvent, type: "mousemove", _defaultPrevented: false, …}
featureTarget: bs {type: "Feature", _vectorTileFeature: qa, properties: {…}, layer: {…}, source: "mapbox-gl-draw-cold", …}
lngLat: B {lng: 172.4191589376344, lat: -43.49662499361186}
originalEvent: MouseEvent {isTrusted: true, screenX: 561, screenY: 524, clientX: 561, clientY: 421, …}
point: c {x: 442, y: 289}
target: Map {_moving: false, _zooming: false, transform: wi, _bearingSnap: 7, _renderFrameCallback: ƒ, …}
type: "mousemove"
_defaultPrevented: false
defaultPrevented: (...)
__proto__: F
undefined
***

我非常困惑...第二行显示featureTarget成员,但第三行中未定义该成员!如您所见,没有错字。

有人对我为什么无法获得featureTarget值有任何想法吗?

1 个答案:

答案 0 :(得分:0)

如果不进行调查,我会说有时候在这种情况下,Chrome和Firefox可能会给出非常令人误解的结果-预览(登录对象时)与形式上将属性评估为字符串并不完全相同。我猜想featureTarget是一个吸气剂,在这种情况下,它的评估值为undefined

真正了解发生情况的更好方法是在该函数中放置一个断点,并分别评估每个表达式,直到获得更清晰的图片为止。