问题描述
我发现在 MAPBox 中很难将经纬度转换为地址名称。我已经为 @R_502_3134@ 找到了 JavaScript,而且它相当简单,但是我找不到 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,具体来说就是reverse geocoding。您必须编写您的网址并使用地址作为数据类型优化结果始终考虑根据您的参数,您可能会在 featureCollection 响应中获得多个结果。
这里有一个给定坐标 [-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
这将返回以下内容
{"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,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",{"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."}