OpenLayers OpenLayers

Changeset 6571

Show
Ignore:
Timestamp:
03/21/08 11:45:54 (5 months ago)
Author:
crschmidt
Message:

Use the more modern click handler to interact with click events rather thanregistering directly on the map. This gives more flexibility and functionalityto applications, and should be the preferred way to handle these events goingforward.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/examples/click.html

    r6497 r6571  
    1313        <script src="../lib/OpenLayers.js"></script> 
    1414        <script type="text/javascript"> 
     15OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {                 
     16                defaultHandlerOptions: { 
     17                    'single': true, 
     18                    'double': false, 
     19                    'pixelTolerance': 0, 
     20                    'stopSingle': false, 
     21                    'stopDouble': false 
     22                }, 
     23 
     24                initialize: function(options) { 
     25                    this.handlerOptions = OpenLayers.Util.extend( 
     26                        {}, this.defaultHandlerOptions 
     27                    ); 
     28                    OpenLayers.Control.prototype.initialize.apply( 
     29                        this, arguments 
     30                    );  
     31                    this.handler = new OpenLayers.Handler.Click( 
     32                        this, { 
     33                            'click': this.trigger, 
     34                        }, this.handlerOptions 
     35                    ); 
     36                },  
     37 
     38                trigger: function(e) { 
     39                    var lonlat = map.getLonLatFromViewPortPx(e.xy); 
     40                    alert("You clicked near " + lonlat.lat + " N, " + 
     41                                              + lonlat.lon + " E"); 
     42                }, 
     43 
     44            }); 
    1545            var map; 
    1646            function init(){ 
     
    3060                // map.setCenter(new OpenLayers.LonLat(0, 0), 0); 
    3161                map.zoomToMaxExtent(); 
    32                 map.events.register("click", map, function(e) { 
    33                     var lonlat = map.getLonLatFromViewPortPx(e.xy); 
    34                     alert("You clicked near " + lonlat.lat + " N, " + 
    35                                               + lonlat.lon + " E"); 
    36                 }); 
     62                 
     63                var click = new OpenLayers.Control.Click(); 
     64                map.addControl(click); 
     65                click.activate(); 
     66 
    3767            } 
    3868        </script> 
     
    4575 
    4676        <p id="shortdesc"> 
    47             This example shows the use of the register and getLonLatFromViewPortPx functions to trigger events on mouse click. 
     77            This example shows the use of the click handler and getLonLatFromViewPortPx functions to trigger events on mouse click.  
     78 
    4879        </p> 
    4980 
    5081        <div id="map"></div> 
    5182     
    52         <div id="docs"></div> 
     83        <div id="docs"> 
     84            Using the Click handler allows you to (for example) catch clicks without catching double clicks, something that standard browser events don't do for you. (Try double clicking: you'll zoom in, whereas using the browser click event, you would just get two alerts.) This example click control shows you how to use it.  
     85        </div> 
    5386    </body> 
    5487</html>