【问题标题】:Listen the click on the "X" button of a MapBox Geocoder聆听点击 MapBox 地理编码器的“X”按钮
【发布时间】:2020-11-18 15:06:56
【问题描述】:

我想使用 MapBox 地理编码器(地图外)来可视化和隐藏/清除地图中的一个点。

每当用户搜索地址时, 我用

geocoder.on('result', function (e) {...}

创建一个点,填充一个图层并将其可视化在地图上。

我想在用户点击“x”按钮时清除/清理图层。

Clear Mapbox geocode

关于如何监听 MapBox 地理编码器的“X”按钮的点击有什么想法吗?

我看过doc,但到目前为止运气不佳!

我的代码如下所示:

function point (){
geocoder.on('result', function (e) {
    console.log(e)
    if (e.result.place_name && e.result.center) {
        var point = create_feature(e.result.place_name, e.result.center[0], e.result.center[1])

        //Add points on the map
        map.getSource('blablabla').setData({
            type: 'FeatureCollection',
            features: [point]
        });
    }

})
};

function create_feature(userId, x, y) {
    var user_feature = {
        'type': 'Feature',
        'properties': {
            'description': userId
        },
        'geometry': {
            'type': 'Point',
            'coordinates': [x, y]
        }
    };
    return user_feature;
};

【问题讨论】:

    标签: mapbox mapbox-gl-js geocoder


    【解决方案1】:

    https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#on

    可用事件和传入各自事件的数据 对象是:

    • clear 当输入被清除时发出

    所以这应该有效:

    geocoder.on('clear', function () {
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多