使用Leaflet.js将用户输入的位置添加到OpenStreetMap中

问题描述 投票:0回答:1

我正在尝试创建用户输入位置的地图,但是,为此,我需要将输入的城市转换为其纬度和经度。这使我可以使用L.marker(point).addTo(map)

使用Leaflet.js轻松绘制它们

考虑到说“伦敦”的输入,是否可以使用OpenStreetMap API来获取这些值?这里有很少的API工具可以完成此任务(根据我的研究),而无需API密钥。

[如果可能的话,我想避免使用google maps API,并且希望获得帮助,无论是API建议还是OpenStreetMap想法,因为我对JavaScript还是比较陌生的。

提前感谢!

javascript leaflet openstreetmap latitude-longitude dc.leaflet.js
1个回答
0
投票

按照评论中的建议,解决方案是使用提名,尤其是使用他们的search queries

我最终使用jQuery的getJson函数从API中获取数据,如下所示。

$(function () {
  geocoding = function (city, country) {
    var url = `https://nominatim.openstreetmap.org/search?city=${city}&country=${country}&format=json`
    $.getJSON(url, function (data) {
      //data is the JSON string
      lat = data[0].lat
      lon = data[0].lon
      ...
    });
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.