【问题标题】:Vue Set Data on Mapbox ClickVue在Mapbox上设置数据点击
【发布时间】:2019-03-30 15:06:09
【问题描述】:

我正在使用 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)


        })

【问题讨论】:

    标签: vue.js vuejs2 mapbox mapbox-gl-js mapbox-marker


    【解决方案1】:

    这是一个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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 2021-01-25
      • 2019-09-03
      • 2022-12-30
      • 2022-01-07
      • 1970-01-01
      相关资源
      最近更新 更多