admin管理员组文章数量:1429928
I use the Mapbox Geocoder and additionally several select menus to customize a map. Now I want to reset the geocoder as soon as the user selects a value from the select menu - so the input field should be cleared and the marker removed.
In the Docs a clear function is described and there is also a clear button in the default input field, but this function doesn't work if I call it from a change event.
This is what my attemp looks like so far:
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();
});
I expect the reset of the input field and the removal of the marker, but the actual output is "geocoder.clear is not a function"
I use the Mapbox Geocoder and additionally several select menus to customize a map. Now I want to reset the geocoder as soon as the user selects a value from the select menu - so the input field should be cleared and the marker removed.
In the Docs a clear function is described and there is also a clear button in the default input field, but this function doesn't work if I call it from a change event.
This is what my attemp looks like so far:
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();
});
I expect the reset of the input field and the removal of the marker, but the actual output is "geocoder.clear is not a function"
Share Improve this question asked May 7, 2019 at 8:02 agsslagssl 1532 silver badges12 bronze badges 03 Answers
Reset to default 2I had a similar issue with a react app that uses mapbox-gl. I got around it by defining the marker outside of the geocoder.on call. This is all inside of the ponentDidMount method of the map ponent.
let marker = new mapboxgl.Marker({ draggable: false, color: "green" })
geocoder.on('result', function(e) {
marker.setLngLat(e.result.center).addTo(map)
});
I used the geocoder.clear() function inside geoder.on('result', ...), and it is working. However, the geocoder is cleared and it is on focus. So, I added another line to lose the focus from the input.
geocoder.on('result', function(e) {
geocoder.clear();
document.getElementsByClassName('mapboxgl-ctrl-geocoder--input')[0].blur();
...
});
const geocoder = new MapboxGeocoder({
accessToken: accessToken,
mapboxgl: mapboxgl,
marker:false,
});
geocoder.on('result', (result) => {
// add marker via result data by your self
})
本文标签: javascriptHow to clear the mapbox geocoderStack Overflow
版权声明:本文标题:javascript - How to clear the mapbox geocoder? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745546593a2662732.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论