简体   繁体   English

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

[英]Geocoding with osm map, javascript code and OpenLayers library

I want to Geocoding using OSM map, javascript code and OpenLayers library. 我想使用OSM地图,javascript代码和OpenLayers库进行地址解析。 .But I do not know what library I should use or what code to write.please guide me. 但是我不知道我应该使用哪个库或编写什么代码。请指导我。 Thanks I want to make changes to the code below to add Geocoding. 谢谢,我想对以下代码进行更改以添加地址解析。

  <!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>

If you're looking for controls to let the user search places on your map, have a look at this example with Nominatim or this with Photon API . 如果您正在寻找让用户在地图上搜索位置的控件,请使用Nominatim使用Photon API的 示例 Just create a control and add it to the map. 只需创建一个控件并将其添加到地图即可。 Then do something when a place is selected (center the map): 然后在选择地点后做点什么(使地图居中):

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

If you want to geocode a bulk of addresses, look at API used by the example and their implementation in the lib. 如果要对大量地址进行地理编码,请查看示例使用的API及其在lib中的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM