Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Im trying to perform routing with openlayers, geoserver and mysql. In order to get the start and end points, I need to be able to get a feature of the node layer (which has point features) that lies closest to an arbitrary point I click on the map. Does openlayers provide any functionality for this. If not, kindly suggest any alternative methods for achieving this.

share|improve this question
What routing engine are you using? Routing data is in mySql? – user1702401 May 22 '13 at 8:09
I haven't made use of any routing engine. MySql does not provide any support for routing unlike pgsql. The algorithm was developed and implemented on the server side in java and the result was visualized with openlayers. – Nik May 22 '13 at 9:48
Thanks for the example! I'm looking for smth similar but I want to get the closest feature while I am drawing a line. Can someone help me please? – m.virdol Aug 6 '14 at 15:38
up vote 5 down vote accepted

With a bit of comparison you can:

  1. detect the point clicked
  2. find the feature closest to this point
  3. use the selectFeatureControl to programatically select this feature


(note, I use underscore.js _.min function, you could of course do this with a foor loop etc, the clue is to loop the features and get the one with the shortest distanceTo the clicked point)

share|improve this answer
Thanks a lot. This was exactly what I had been looking for. Thanks again for providing the perfect example. Greetings. – Nik May 22 '13 at 9:38
Glad to be able to help. Perhaps you could accept my answer then? – atlefren May 22 '13 at 11:52
@atlefren +1 nice use of jsfiddle (interactive code) and solves rashad question clearly. – Mapperz May 22 '13 at 13:35

Yes, OpenLayers has this functionality. Its called SelectFeature Control. see docs here.

you should check how the following examples work(uses: OpenLayers.Control.SelectFeature).

  1. OpenLayers Select and Highlight Feature Example
  2. OpenLayers Select Feature Example

You can get full list of examples here and apply keywords filter for select or higlight

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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