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

In Openlayers, I want on a single click on the map for a layer to be added and a feature to be dropped where I click.

vectorLayer = new OpenLayers.Layer.Vector("layer", {styleMap: currentStyle,'displayInLayerSwitcher': true});
map.addLayer(vectorLayer);
drawFeature = new OpenLayers.Control.DrawFeature(vectorLayer, OpenLayers.Handler.Point);
map.addControl(drawFeature);

I think this should probably be done with events somehow. But every option I try invovles a double click - one to add the layer, and one to add the feature. Anybody know how to solve this crisis? Thanks!

share|improve this question
up vote 2 down vote accepted

Only you need to do is to bind a OnClick event on the map and process what you want:

function bindClickButton() {
    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
            );
            OpenLayers.Control.prototype.initialize.apply(
                this, arguments
            ); 
            this.handler = new OpenLayers.Handler.Click(
                this, {
                    'click': this.trigger
                }, this.handlerOptions
            );
        }, 

        trigger: function(e) {
            var lonlat = map.getLonLatFromPixel(e.xy);
            lonlat.transform( map.projection,map.displayProjection);
            processMouseClick(lonlat);
        }

    });

    var click = new OpenLayers.Control.Click();
    map.addControl(click);
    click.activate();

}

So, create the processMouseClick() to process the click :

function processMouseClick(lonlat) {
    // DO_ALL_YOUR_JOB_HERE
}

Just ignore my projection transform if not in your case.

share|improve this answer
    
Remember : var lon = lonlat.lon; var lat = lonlat.lat; – Magno C Jan 4 '13 at 10:52
    
In your way, every click on map adds a new layer. Is this what you realy want? Be careful. – Magno C Jan 4 '13 at 11:55
    
awesome thanks a lot! – KyleK Jan 9 '13 at 17:46

Your Answer

 
discard

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.