mapboxmapbox-gl-jsgeocode

Retrieve data from geocoder MapboxGl (Results)


So I found a solution on how to get the results of geocoder from a different post.

AndrewHarvey:

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);
})

AndrewHarvey's solution in the link: Retrieve data from mapbox geocoder has gotten me to see in the console the coordinates but i'm just not quite sure on how to store those. I'll show in the screenshots below.

In the 2nd screenshot you can see where i went into "config" and then there was "proximity" and those are the coordinates I need to store into a variable in my javascript code in order to compare to a set pair of coordinates I have.

In the 3rd screenshot there is the "features" tab and it has a bunch of arrays with the first array being the location I requested, I just need to get the center, (The coordinates) of that location and store them. If anyone can help me thanks!

1st Screenshot

2nd Screenshot

3rd Screenshot


Solution

  • So I was able to figure it out on my own by just playing around with calling the array and then finding it by trial and error.

    geocoder.on('results', function(results) {
         console.log(results.features[0].center[0]);
         console.log(results.features[0].center[1]);
    });