【问题标题】:Convert the lat and long to address name in MAPBOX GL JS在 MAPBOX GL JS 中将 lat 和 long 转换为地址名称
【发布时间】:2021-01-27 01:08:59
【问题描述】:

我发现很难将 lat 和 long 转换为 MAPBOX 中的地址名称。我找到了适用于 google api 的 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)



【问题讨论】:

    标签: javascript mapbox reverse-geocoding


    【解决方案1】:

    你需要使用Mapbox Geocoding API,具体是reverse geocoding。您必须编写您的 url 并使用地址作为数据类型细化结果始终考虑您可以在 featureCollection 响应中获得多个结果,具体取决于您的参数。

    这里有一个给定坐标 [-73.989, 40.733] 的示例 URL,对应于 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, 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."}
    

    【讨论】:

    • 您好,感谢您的回答!!我得到了您坐标的地址,但是当我将坐标更改为我的值时,它不会返回地址:(我的 long 和 lat 值是 116.078063888889, -33.8620819444444 。我还尝试交换它们。只是为了让你知道当我将链接复制并粘贴到浏览器中时会得到这个结果
    • 我也试过在你的坐标中加入 -72 而不是 -73,但它不起作用。似乎只适用于特定坐标?
    • 您使用的 lnglat 位置对应于澳大利亚布里奇敦附近的一条道路,对吧?我在那里没有看到任何地址。
    • 但是当我使用在线转换器并输入纬度和经度时,我得到了一个地址。但是你认为 mapbox API 只是给出了房子地址的详细信息吗?因为我试图提供跟踪设备的位置地址。 latlong.net/Show-Latitude-Longitude.html
    • 然后试试这个网址,我删除了 types=Address 过滤器api.mapbox.com/geocoding/v5/mapbox.places/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多