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

时间:2015-06-19 09:15:08

标签: javascript leaflet geocoding

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

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

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

任何人都可以提供一个查找地址的示例(通过OSMN或其他内容,而不是谷歌/ bing或其他需要api-key的提供商),将其转换为lon / lat并在地图上添加标记?

2 个答案:

答案 0 :(得分:5)

首先,您必须在HTML代码的头部包含地理编码器的.js文件,对于我使用此示例的示例:https://github.com/perliedman/leaflet-control-geocoder。像这样:

<script src="Control.Geocoder.js"></script>

然后你必须在你的.js中初始化Geocoder:

geocoder = new L.Control.Geocoder.Nominatim();

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

var yourQuery = (Addres of person);    

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

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

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);
});

答案 1 :(得分:3)

我做了一个jfsfiddle

  1. 设置了地址
  2. 使用geosearch
  3. 查找该地址的坐标
  4. 在geosearch找到的地址坐标处创建标记。
  5. 可在此处找到:https://jsfiddle.net/Alechan/L6s4nfwg/

    “棘手”的部分是处理geosearch返回的Javascript“Promise”实例,并且该地址可能是不明确的,并且在这种情况下可能会返回多个坐标。另外,要小心,因为Leaflet坐标中的第一个位置对应于纬度,第二个位置对应于经度,这与Geosearch“x”和“y”结果相反。

    Geosearch返回一个promise,因为它是一个异步调用。替代方案必须是同步调用,浏览器必须冻结,直到检索到答案。有关promises from 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>
    
相关问题