使用 osm 地图、javascript 代码和 OpenLayers 库进行地理编码

Geocoding with osm map, javascript code and OpenLayers library

我想使用 OSM 地图、javascript 代码和 OpenLayers 库进行地理编码。 .但我不知道我应该使用什么库或什么代码来 write.please 指导我。 谢谢 我想更改下面的代码以添加地理编码。

  <!DOCTYPE html>
    <html>
      <head>
        <title>Show OSM map</title>
        <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
        <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
        <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
        <style>
          html, body {
            height: 100%;
            width: 100%;
        padding: 0px;
        margin: 0px;
          } 
          .map {
            height: 90%;
            width: 100%;
          }
        </style>
        <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
      </head>
      <body>
        <div id="map" class="map" "></div>
        <script>

          var map = new ol.Map({

            layers: [
              new ol.layer.Tile({
                source: new ol.source.OSM()
              })
            ],
            target: 'map',
            view: new ol.View({
              center: ol.proj.fromLonLat([51.39, 35.70]),
              zoom: 13
            })
          });
        </script>

    </script> 
      </body>
    </html>

如果您正在寻找让用户在您的地图上搜索地点的控件,请查看此 example with Nominatim or this with Photon API。 只需创建一个控件并将其添加到地图中。然后在选择地点后做一些事情(将地图居中):

// Set the search control 
var search = new ol.control.SearchNominatim ({
// or: = new ol.control.SearchPhoton({
  lan: 'en',
  position: true    // Search, with priority to geo position
});
map.addControl (search);

// Center map when destination is selected
search.on('select', function(e) {
  map.getView().animate({
    center: e.coordinate,
    zoom: Math.max (map.getView().getZoom(),16)
});

如果您想对大量地址进行地理编码,请查看示例使用的 API 及其在库中的实现。