[OpenLayers-Users] Deactivate a control by its callback

Tim Schaub tschaub at opengeo.org
Sat Apr 18 17:50:49 EDT 2009


Hey-

pribram pribram wrote:
> Hi,
> I would like to deactivate the control based on OpenLayers.Control.Measure inside its own callback function. After the second click the error appears - "this.point.geometry' is null or not an object" on this.callback("modify",[this.point.geometry,this.getSketch()]);
> 

If you are using the version in the trunk, you should be able to use the 
control as is, register for measurepartial, and deactivate the control 
when you reach the number of points you want.  I think we discussed this 
on IRC.  Maybe I'm remembering wrong.

Try this:

control.events.on({
     measurepartial: function(event) {
         if(event.geometry.components.length > 2) {
             control.deactivate();
         }
     }
});

This will only work after r9263.

Tim

> My code:
>         var map;
>         function init(){
>             map = new OpenLayers.Map('map', {projection: new OpenLayers.Projection("EPSG:900913")});
> 
>             var ghyb = new OpenLayers.Layer.Google(
>                 "Google",
>                 { type: G_SATELLITE_MAP }
>             );
> 
>             map.addLayers([ghyb]);
> 
>             var madrid = new OpenLayers.LonLat(5, 40).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
>             map.setCenter(madrid, 5);
> 
>             var ctrl = new OpenLayers.Control.MyMeas(OpenLayers.Handler.Path);
>             map.addControl(ctrl);
>             ctrl.activate();
>         }
> 
>         OpenLayers.Control.MyMeas = OpenLayers.Class(OpenLayers.Control.Measure, {
>             clicks: 0,
> 
>             draw: function() {
>                 this.handler.callbacks = { point: this.point };
>             },
> 
>             point: function(pnt, geom) {
>                 switch (this.clicks) {
>                     case 0:
>                         this.clicks++;
>                         break;
>                     case 1:
>                         this.deactivate();
>                         break;
>                 }
>             }
> 
>         });
> 
> 
> Thank you for any idea.
> 
> Pribram 
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users


-- 
Tim Schaub
OpenGeo - http://opengeo.org
Expert service straight from the developers.



More information about the Users mailing list