我使用Mapbox Geocoder和其他几个选择菜单来自定义 map 。现在,我想在用户从选择菜单中选择一个值后立即重置地址解析器-因此应清除输入字段并删除标记。

Docs中,描述了一个清除函数,并且默认输入字段中还有一个清除按钮,但是如果我从change事件中调用它,此函数将不起作用。

到目前为止,这是我的尝试:


const select  = $('#select');
const control = $('#geocoder');

const geocoder = new MapboxGeocoder({
 accessToken: accessToken,
 mapboxgl: mapboxgl
});

//Add the geocoder
document.getElementById(control).appendChild(geocoder.onAdd(map));

//Now reset the geocoder if the user selects something
select.change(function() {
 geocoder.clear();
});

我希望可以重置输入字段并删除标记,但是实际输出是“geocoder.clear不是函数”

最佳答案

我在使用mapbox-gl的React应用程序中遇到了类似的问题。我通过在geocoder.on调用之外定义标记来解决此问题。这都在 map 组件的componentDidMount方法内部。

let marker = new mapboxgl.Marker({ draggable: false, color: "green" })

geocoder.on('result', function(e) {
  marker.setLngLat(e.result.center).addTo(map)
});

关于javascript - 如何清除 map 框地理编码器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56018065/

10-12 15:47