从 mapbox 地理编码器中检索数据

Retrieve data from mapbox geocoder

我只需要没有地图的 mapbox 地理编码自动完成(将 lat/lng 的结果放在另一个请求中)

我设法在没有地图的情况下完全独立地使用它:

<template>
    <div id='geocoder' class='geocoder'></div>
</template>

<script>
import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder'
require('../../node_modules/@mapbox/mapbox-gl-geocoder/dist/mapbox-gl-geocoder.css')

mapboxgl.accessToken = '<your access token here>';
var geocoder = new MapboxGeocoder({
    accessToken: mapboxgl.accessToken
    placeholder: 'Rechercher'
});
document.getElementById('geocoder').appendChild(geocoder.onAdd());
</script>

但现在我想检索数据(特别是 lat/lng 属性,以便将其保存在我的组件中并使用它)

我该怎么做?我搜索了 mapbox 文档,但没有找到任何相关信息:/

提前感谢社区

我终于找到了一种更便宜、更易于集成的替代方案:

https://community.algolia.com/places/documentation.html#using-npm

这是简单的代码:

<template>
    <input type="search" id="address-input" placeholder="Where are we going?" />
</template>

<script>
import Places from 'places.js'

...
    mounted: function() {
        var placesAutocomplete = new Places({
            container: document.querySelector('#address-input')
        })
        placesAutocomplete.on('change', e => console.log(e.suggestion))
    }
</script>

从 API 文档 https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#on 您可以使用

geocoder.on('results', function(results) {
   console.log(results);
})