Search code examples
geometrycoordinatesmapboxlayergeocode

Get coordinates of Layer-circle


I am using Mapbox geocode and I would like to print the coordinates of the created circle in the console but I dont know how I can access it. Can someone help me? Somehow I have to access to result I think but I dont know which command i have to use.

var geocoder = new MapboxGeocoder({
    accessToken: mapboxgl.accessToken,
    mapboxgl: mapboxgl,
    marker: false,
    zoom: 13
});

// After the map style has loaded on the page,
// add a source layer and default styling for a single point
mymap.on('load', function() {
    mymap.addSource('single-point', {
        type: 'geojson',
        data: {
            type: 'FeatureCollection',
            features: []
        }
    });
});

// Listen for the `result` event from the Geocoder
// `result` event is triggered when a user makes a selection
//  Add a marker at the result's coordinates
geocoder.on('result', function(e) {

    mymap.addLayer({
        id: 'point',
        source: 'single-point',
        type: 'circle',
        paint: {
            'circle-radius': 10,
            'circle-color': '#448ee4'
        }
    })
    
    mymap.getSource('single-point').setData(e.result.geometry);
    
    console.log(e.result.lngLat); /*this line*/
    currentMarkers[0].remove();
});


document.getElementById('geocode').appendChild(geocoder.onAdd(mymap));

Solution

  • You can access the geometry result: e.result.geometry.coordinates