首页 > 解决方案 > 获取图层圆的坐标

问题描述

我正在使用 Mapbox 地理编码,我想在控制台中打印创建的圆的坐标,但我不知道如何访问它。有人能帮我吗?不知何故,我必须访问我认为的结果,但我不知道我必须使用哪个命令。

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

标签: geometrycoordinatesmapboxlayergeocode

解决方案


您可以访问几何结果: e.result.geometry.coordinates


推荐阅读