我正在使用 Mapbox 地理编码器来搜索我的 Mapbox 项目之一中的位置。我能够成功实现搜索功能,但我很难从搜索/建议地址列表中提取用户选择的地址。
有没有办法获取搜索地点的坐标?
我浏览了mapbox文档,但只发现了一个事件“moveend”,当我们飞到某个位置时会触发该事件。所以我正在监听该事件,一旦触发,我就会在 map 上调用 getBounds() 函数。它以某种方式工作,但它没有给我确切的坐标,因为 getBounds() 给出了盒子的西南和东北坐标。我们能得到所选地点的精确坐标吗?
下面是我用来搜索地点的代码。
initMapSearch() {
var _this = this;
var geocoder = new MapboxGeocoder({ accessToken: environment.mapbox.accessToken, mapboxgl: mapboxgl });
this.locationObj = geocoder.onAdd(_this.map)
document.getElementById('geocoder').appendChild(this.locationObj);
}
这是我的 moveend 事件。
this.map.on('moveend', function() {
_this.lat = _this.map.getBounds()["_ne"]["lng"];
_this.long = _this.map.getBounds()["_ne"]["lat"];
});
提前感谢社区。p>
最佳答案
您应该监听传递要素(及其位置)的 geocoder.on('result')
。
关于mapbox - 有什么方法可以从 Mapbox 中搜索的位置检索坐标吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61808963/