簡體   English   中英

打開圖層地圖,經度和緯度獲取地址

[英]Open layers maps, with longitude and latitude get address

我想用經度和緯度獲取地址(城市,郵政編碼,街道地址),但我不知道如何。 我正在使用Open層,當我點擊地圖的一部分時,獲得該位置的經度和緯度。 有人有解決方案嗎?

 <!DOCTYPE html> <html> <head> <title>Mouse Position</title> <link rel="stylesheet" href="https://openlayers.org/en/v4.1.0/css/ol.css" type="text/css"> <script src="https://openlayers.org/en/v4.1.0/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: [2918616.660738325,4873187.634331534], zoom: 19 }) }); map.on('click', function (evt) { var coord = ol.proj.toLonLat(evt.coordinate); alert(coord); }); </script> </body> </html> 

您正在尋找的是“反向地理編碼”。 這里顯示了一個很好的例子: https//gist.github.com/ThomasG77/26e61508217ba86a04c19a67cbda0e99

在你的情況下,大致翻譯為:

function reverseGeocode(coords) {
   fetch('http://nominatim.openstreetmap.org/reverse?format=json&lon=' + coords[0] + '&lat=' + coords[1])
     .then(function(response) {
            return response.json();
        }).then(function(json) {
            console.log(json);
        });
}

map.on('click', function (evt) {
  var coord = ol.proj.toLonLat(evt.coordinate);
  reverseGeocode(coord);
});

然后響應將類似於:

{  
   "place_id":"109922016",
   "licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright",
   "osm_type":"way",
   "osm_id":"174849788",
   "lat":"40.0498172",
   "lon":"26.2183522194416",
   "display_name":"Çanakkale Martyrs' Memorial, D550, Seddülbahir, Eceabat, Çanakkale, Marmararegion, Türkei",
   "address":{  
      "memorial":"Çanakkale Martyrs' Memorial",
      "road":"D550",
      "village":"Seddülbahir",
      "county":"Eceabat",
      "state":"Marmararegion",
      "country":"Türkei",
      "country_code":"tr"
   },
   "boundingbox":[  
      "40.0496644",
      "40.0499658",
      "26.2181747",
      "26.2185707"
   ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM