简体   繁体   中英

mapbox-gl: Show reverse geocoding results in popup?

I'm using mapbox-gl's API to create a mapbox view w/ a popup. I'd like to know how I can show my coordinates ( birdLocation variable) as an address, like this: New York, NY 11208

renderMap() {
    if (this.props.bird.location) {
        let birdLocation = this.props.bird.location;
        let map = new mapboxgl.Map({
            container: 'mapbox-container',
            style: config.mapbox.style,
            center: birdLocation,
            zoom: 13,
            interactive: false,
            preserveDrawingBuffer: true
        });
        let popup = new mapboxgl.Popup()
            .setLngLat(birdLocation)
            .setHTML(
                '<div class="location-popup location-title">BIRD LOCATION</div><div class="location-popup location-address">' + {birdLocation} + '</div><a href="/trips" class="location-popup location-plan">PLAN A TRIP</a>'
            )
            .addTo(map);

        map.on('load', function () {
            map.addSource("points", {
                "type": "geojson",
                "data": {
                    "type": "FeatureCollection",
                    "features": [{
                        "type": "Feature",
                        "geometry": {
                            "type": "Point",
                            "coordinates": birdLocation
                        },
                        "properties": {
                            "icon": "dark-map-pin"
                        }
                    }]
                }
            });

            map.addLayer({
                "id": "points",
                "type": "symbol",
                "source": "points",
                "layout": {
                    "icon-image": "{icon}"
                }
            });
        });
    }
},

What you're looking for is reverse geocoding, which turns a coordinate, like birdLocation , into a name and/or address. Here's Mapbox's API for reverse geocoding that you can use with any AJAX library in order to get a result.

disclosure: I work at Mapbox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM