Leaflet.Geosearch:从地址获取lon/lat

Gro*_*den 8 javascript geocoding leaflet

在没有任何JS知识的情况下,我被迫在网页上实现了一个地图(OSM via Leaflet).在此地图上,应该有一个人的实际地址标记.地址将作为字符串保存在数据库中.我可以看到地图,可以添加标记,但在那之后,我迷路了.

我已经测试了一些Leaflet-geocoding-plugins,但我必须承认,它们对我的实际编程体验来说不够简单.

另一个问题是关于同样的问题,但我不明白,如何使用L.Geosearch -plugin for Leaflet 从一个地址获取lon/lat .

任何人都可以给我一个查找地址的例子(通过OSMN或其他东西,而不是google/bing或其他api-key-needy提供商),将其转换为lon/lat并在地图上添加标记?

Tit*_*men 6

首先,您必须在HTML代码的开头包含地址解析器的.js文件,对于我的示例,我使用了以下代码:https : //github.com/perliedman/leaflet-control-geocoder。像这样:

<script src="Control.Geocoder.js"></script>
Run Code Online (Sandbox Code Playgroud)

然后,您将必须在.js中初始化Geocoder:

geocoder = new L.Control.Geocoder.Nominatim();
Run Code Online (Sandbox Code Playgroud)

然后,您将必须指定要查找的地址,然后将其保存在变量中。例如:

var yourQuery = (Addres of person);    
Run Code Online (Sandbox Code Playgroud)

(您也可以从数据库中获取地址,然后将其保存在变量中)

然后,您可以使用以下代码将地址“地理编码”为纬度/经度。此函数将返回地址的纬度/经度。您可以将纬度/经度保存在变量中,以便以后将其用于标记。然后,您只需要将标记添加到地图。

geocoder.geocode(yourQuery, function(results) {    
       latLng= new L.LatLng(results[0].center.lat, results[0].center.lng);
       marker = new L.Marker (latLng);
       map.addlayer(marker);
});
Run Code Online (Sandbox Code Playgroud)


Ale*_*han 5

我做了一个jfsfiddle

  1. 设置了地址
  2. 使用geosearch查找该地址的坐标
  3. 在geosearch找到的该地址的坐标处创建一个标记。

可以在这里找到:https : //jsfiddle.net/Alechan/L6s4nfwg/

“棘手的”部分处理的是geosearch返回的Javascript“ Promise”实例,该地址可能是模棱两可的,在这种情况下可能返回多个坐标。另外,请注意,因为“传单”坐标中的第一个位置与纬度相对应,第二个位置与经度相对应,这与地理搜索的“ x”和“ y”结果相反。

Geosearch返回了一个Promise,因为它是一个异步调用。替代方案将是同步调用,浏览器将被冻结,直到检索到答案为止。有关MDM(Mozilla)Google的诺言的更多信息。

在我的示例中,我为为指定地址找到的每个结果创建一个标记。但是,在这种情况下,该地址是明确的,并且仅返回一个结果。

代码明细:

<!-- Head, imports of Leaflet CSS and JS, Geosearch JS, etc -->

<div id='map'></div>


<script>
// Initialize map to specified coordinates
  var map = L.map( 'map', {
    center: [ 51.5, -0.1], // CAREFULL!!! The first position corresponds to the lat (y) and the second to the lon (x)
    zoom: 12
});

  // Add tiles (streets, etc)
  L.tileLayer( 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
    subdomains: ['a','b','c']
}).addTo( map );

var query_addr = "99 Southwark St, London SE1 0JF, UK";
// Get the provider, in this case the OpenStreetMap (OSM) provider.
const provider = new window.GeoSearch.OpenStreetMapProvider()
// Query for the address
var query_promise = provider.search({ query: query_addr});
// Wait until we have an answer on the Promise
query_promise.then( value => {
   for(i=0;i < value.length; i++){
     // Success!
     var x_coor = value[i].x;
     var y_coor = value[i].y;
     var label = value[i].label;
     // Create a marker for the found coordinates
     var marker = L.marker([y_coor,x_coor]).addTo(map) // CAREFULL!!! The first position corresponds to the lat (y) and the second to the lon (x)
     // Add a popup to said marker with the address found by geosearch (not the one from the user)
     marker.bindPopup("<b>Found location</b><br>"+label).openPopup();
   };
}, reason => {
  console.log(reason); // Error!
} );

</script>
Run Code Online (Sandbox Code Playgroud)