简体   繁体   English

mapbox-gl:在弹出窗口中显示反向地理编码结果吗?

[英]mapbox-gl: Show reverse geocoding results in popup?

I'm using mapbox-gl's API to create a mapbox view w/ a popup. 我正在使用mapbox-gl的API创建带有弹出窗口的mapbox视图。 I'd like to know how I can show my coordinates ( birdLocation variable) as an address, like this: New York, NY 11208 我想知道如何将坐标( birdLocation变量)显示为地址,例如: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. 您正在寻找的是反向地理编码,它可以将座标(例如birdLocation )转换为名称和/或地址。 Here's Mapbox's API for reverse geocoding that you can use with any AJAX library in order to get a result. 这是Mapbox的反向地理编码API ,您可以将其与任何AJAX库一起使用以获得结果。

disclosure: I work at Mapbox 披露:我在Mapbox工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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