0

我正在使用 Mapbox 在地图点击上放置一个标记。我成功获得了坐标,但是我无法将它们绑定到我的数据...

         map.on('click', function(e) {
            if (this.marker) { this.marker.remove() }
            this.marker = new mapboxgl.Marker()
            .setLngLat({ lng: e.lngLat.lng, lat: e.lngLat.lat})
            .addTo(map);
            map.flyTo({
                center: { lng: e.lngLat.lng, lat: e.lngLat.lat },
                zoom: 15
            });

            // This does not bind and update the data
            this.latitude = JSON.stringify(e.lngLat.lat)
            this.longitude = JSON.stringify(e.lngLat.lng)


        })
4

1 回答 1

4

这是个contextual binding问题。this这里不是指您的实例vue,而是指代map

// fat arrow solves this
map.on('click', function(e) => {

})

// aliasing solves this
const self = this
map.on('click', function(e) {

})

// binding solves this
map.on('click', function(e) => {

}.bind(this))
于 2018-10-25T22:16:37.107 回答