在 MAPBOX GL JS 中将经纬度转换为地址名称

Convert the lat and long to address name in MAPBOX GL JS

我发现很难将纬度和经度转换为 MAPBOX 中的地址名称。我已经找到 JavaScript google api 并且它相当简单,但我找不到 mapbbox 的解决方案。非常感谢任何帮助。我知道应该实施反向编码以将纬度和经度转换为地址,但在参考 mapbox 反向地理编码文档后我很困惑。

归根结底,我只想将标记位置的地址名称存储在 JavaScript 变量中。

JS

mapboxgl.accessToken = 'pk.eyJ1IjoiZXNxdWlsYXgiLCJhIjoiY2tqd2ZyMXJsMGVqeDJ4cW8xcnRja2tqdiJ9.7z7Eyrj3iexJ9uDVYIT0yw';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v11', // stylesheet location

   center: [lng, lat], 
  
    zoom: 11
    
 
});
var marker = new mapboxgl.Marker({color:"#ff3300",dragable:true,scale:0.8}).setLngLat([lng, lat]).addTo(map)



您需要使用Mapbox Geocoding API, concretely reverse geocoding。您必须编写 url 并使用 address 作为数据类型优化结果 始终考虑根据您的参数在 featureCollection 响应中获得多个结果。

这里有一个例子 url 给定的坐标 [-73.989, 40.733] 对应于 120 East 13th Street, New York, New York 10003, United States:

https://api.mapbox.com/geocoding/v5/mapbox.places/-73.989,40.733.json?types=address&access_token=pk.eyJ1IjoianNjYXN0cm8iLCJhIjoiY2s2YzB6Z25kMDVhejNrbXNpcmtjNGtpbiJ9.28ynPf1Y5Q8EyB_moOHylw

这将return以下

{"type":"FeatureCollection","query":[-73.989,40.733],"features":[{"id":"address.694032850198644","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"rooftop"},"text":"East 13th Street","place_name":"120 East 13th Street, New York, New York 10003, United States","center":[-73.9888929,40.7330031],"geometry":{"type":"Point","coordinates":[-73.9888929,40.7330031]},"address":"120","context":[{"id":"neighborhood.2103290","text":"Greenwich Village"},{"id":"postcode.13482670360296810","text":"10003"},{"id":"locality.12696928000137850","wikidata":"Q11299","text":"Manhattan"},{"id":"place.15278078705964500","wikidata":"Q60","text":"New York"},{"id":"region.17349986251855570","wikidata":"Q1384","short_code":"US-NY","text":"New York"},{"id":"country.19678805456372290","wikidata":"Q30","short_code":"us","text":"United States"}]}],"attribution":"NOTICE: © 2021 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare."}