Retrieve data from mapbox geocoder

谁说胖子不能爱 提交于 2019-12-10 11:26:23

问题


i only need the mapbox geocoding autocomplete without the map (to put the result with lat/lng in another request)

I managed to put it totally alone without the map using this :

<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>

But now i would like to retrieve the data (specifically the lat/lng attribute in order to save it in my component and work with it)

How can i do that ? i've search through mapbox doc but did not found anything about that :/

Thanks in advance to the community


回答1:


From the API docs https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#on you can use

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



回答2:


I've finally found a cheaper and easier to integrate alternative :

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

here is the simple code :

<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>


来源:https://stackoverflow.com/questions/51406675/retrieve-data-from-mapbox-geocoder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!