2

Is there a way to get osm_id of the point that is closest to a given coordinate.

The idea is to click on map and get the osm_id

 map.on('click', function(event) {
      window.alert(event.coordinate);
 });
0

2 Answers 2

3

You can by using ol.source getClosestFeatureToCoordindinate() and feature get().

Assuming your OpenStreetMap data is loaded into ol.source.Vector called osm.

map.on('click', function(event) {
  var osm_id = osm.getClosestFeatureToCoordinate(event.coordinate).get('osm_id');
  window.alert(osm_id);
)}
2
  • I think you have to elaborate a little bit. There's no getClosestFeatureToCoordinate for ol.source.OSM. How do you get ol.source.Vector with OSM? Commented May 27, 2015 at 13:07
  • I was working under the idea Alex was using something like this OSM Vector Tiles
    – Andy
    Commented May 27, 2015 at 13:26
2

Returning objects at a given location is possible via Overpass API.

Unfortunately I don't have a full example. But if nobody else comes up with an example I can at least provide you some helpful links:

1
  • Overpass API can only return objects within a certain radius of a coordinate (or a way, etc.). If there's no result in the given proximity of a point (think middle of the ocean), you need to continue querying with a larger radius until you find something. Unfortunately, there's no other way atm to find the closest point for a given point.
    – mmd
    Commented Jun 6, 2015 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.