Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open layers maps, with longitude and latitude get address

I am trying to get address(City, Post code,Street address) with longitude and latitude but i don`t know how. I am using Open layers and when i click on part of map a get longitude and latitude of that position. Does anybody have solution for this?

<!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>
like image 784
Filip Filipovic Avatar asked Jun 15 '18 20:06

Filip Filipovic


People also ask

Can Google maps use latitude and longitude?

To search for a place, enter the latitude and longitude GPS coordinates on Google Maps. You can also find the coordinates of the places you previously found. Besides longitude and latitude, you can use plus codes to share a place without an address.

How do I get coordinates on OpenLayers?

Calling the useGeographic function in the 'ol/proj' module makes it so the map view uses geographic coordinates (even if the view projection is not geographic).

What is Openlayer projection?

What projection is OpenLayers using? Every map that you'll create with OpenLayers will have a view, and every view will have a projection. As the earth is three-dimensional and round but the 2D view of a map isn't, we need a mathematical expression to represent it. Enter projections.


1 Answers

what you are looking for is "reverse geocoding". A good example of what that looks like is shown here: https://gist.github.com/ThomasG77/26e61508217ba86a04c19a67cbda0e99

In your case that would roughly translate to:

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

The response would then look similar to this:

{  
   "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"
   ]
}
like image 78
Marco Avatar answered Oct 13 '22 09:10

Marco