Hello everbody,

Many Thanks for OSM.

Can anybody explain how to perform geocoding with OSM?

I want to get programatically the lat/lon when I make
a click on the map.

Thank you in advance !


Assuming that you use OpenLayers add this to your Javascript:


var click = new OpenLayers.Control.Click();

New function:

OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {                
        defaultHandlerOptions: {
            'single': true,
            'double': false,
            'pixelTolerance': 0,
            'stopSingle': false,
            'stopDouble': false
        initialize: function(options) {
            this.handlerOptions = OpenLayers.Util.extend(
                {}, this.defaultHandlerOptions
                this, arguments
            this.handler = new OpenLayers.Handler.Click(
                this, {
                    'click': this.trigger
                }, this.handlerOptions
        trigger: function(e) {
            position =;
alert("x="+position.x+", y="+position.y);


You can use openlayers (just copy the code for the slippy map on osm homepage). E.g. look on the javascripts of existing projects, one of the osm routing projectsdid it with openlayers.

Basically it’s just adding a small callback called “handle click on map” (or something like that), and you will get a lat/lon.

emj: Double post ??? :smiley:

Strange indeed :roll_eyes: dunno what happen (for the record I deleted it)

Another question for geocoding.

Is there any limitation in the number of geocoding requests per day?

I.E with the google maps API there is a limit of 15.000 per day.

Thank you!

OpenStreetMap doesn’t aim to provide a resilient public API for data consumers. The OpenStreetMap API is primarily (though not entirely) there for the use of those editing the map. If you want to fire a large number of requests at the API, you should consider downloading planet.osm and serving the requests yourself; using OSMXAPI; or engaging a company that provides services from OSM data, such as Geofabrik or CloudMade.

Manual geocoding as in clicking on a Javascript map is free, since it never touches the API.

Geocoding as in asking the server API what data is around a certain point, or where this street name is located, is too slow to be considered free

I’m a bit unsure what geocoding actually is in this instance.

If you’re talking about getting the coordinate of a certain point on the map then yes, geocoding is free because the client side javascript can do that very easy. If you’re firing requests to the Google API, or OSM API then it won’t be free (Google because it ain’t ‘free’, OSM because applications/mashups that use the map a lot are supposed to host their own).

Many thanks for your replies, everything is much clearer now :slight_smile:


Wish you a Happy X’ Mas & New Year.

I am very new to OpenLayers API. actually i would like to plug the open street map view in to my .net based application and perform the Geocoding by user requested address. kindly let me know how to plug, and will it possible to do geocoding using openlayer API.

Can i have any basic example or any doc openlayer API.

Thanks & Regards,

I’m using just about the same code but determin the position with

    trigger: function(evt) {
        var lonlat = map.getLonLatFromPixel (evt.xy).transform (projMercator, projWGS84);

What’s a advantage of your solution?



BTW how can I apply MOD_CTRL to the handler options so that only ctrl-click triggers the event?