How to stop registering events

5 messages Options
Embed this post
Permalink
Max Stephan

How to stop registering events

Reply Threaded More More options
Print post
Permalink
Hi list,

I´m using layer.events.register('loadend',map,zoomToFeature); in my application. The scenario is the following:
In a list the user clicks on an icon that represents a polygon-Feature and has an ID. After that I´m querying my Geoserver for the feature with this ID and get the response back. From this I can calculate the bounds of this geometry and zoom to it.
Now I want to select this layer because most of the features are surrounded by other features to show the user which one he selected in the list. For this to happen I have to wait till the layer is loaded (it contains all the features of the same featureclass that I´m querying from the Geoserver). Then I´m selecting the feature via selectControl.select(//FeatureObject//);.
The problem is: The script keeps on registering the loadend event and for that reason always reactivates the queried feature (for example when I click on another feature).
So, what I need is a way to deactivate the registering after the first registration of the event. I tried layer.events.unregister('loadend',map,zoomToFeature); but that´s not working. In addition I´m not completely sure which params I should give to the unregister-function and what the unregister-function does.

Thx for your help in advance
Max Stephan
Eric Lemoine-2-2

Re: How to stop registering events

Reply Threaded More More options
Print post
Permalink
On Thu, Oct 29, 2009 at 1:00 PM, Max Stephan <[hidden email]> wrote:

>
> Hi list,
>
> I´m using layer.events.register('loadend',map,zoomToFeature); in my
> application. The scenario is the following:
> In a list the user clicks on an icon that represents a polygon-Feature and
> has an ID. After that I´m querying my Geoserver for the feature with this ID
> and get the response back. From this I can calculate the bounds of this
> geometry and zoom to it.
> Now I want to select this layer because most of the features are surrounded
> by other features to show the user which one he selected in the list. For
> this to happen I have to wait till the layer is loaded (it contains all the
> features of the same featureclass that I´m querying from the Geoserver).
> Then I´m selecting the feature via selectControl.select(//FeatureObject//);.
> The problem is: The script keeps on registering the loadend event and for
> that reason always reactivates the queried feature (for example when I click
> on another feature).
> So, what I need is a way to deactivate the registering after the first
> registration of the event. I tried
> layer.events.unregister('loadend',map,zoomToFeature); but that´s not
> working.

Hi

it should work. I just did the following in the FireBug console:

>>> var cb = function() {}; scope = {};
>>> var e = new OpenLayers.Events({}, null, ["eventtype"]);
>>> e.register("eventtype", scope, cb);
>>> e.listeners["eventtype"].length == 1
>>> e.unregister("eventtype", scope, cb);
>>> e.listeners["eventtype"].length == 0

Cheers,

--
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : [hidden email]
http://www.camptocamp.com
_______________________________________________
Users mailing list
[hidden email]
http://openlayers.org/mailman/listinfo/users
Max Stephan

Re: How to stop registering events

Reply Threaded More More options
Print post
Permalink
Hi Eric,

thanks for your help but I don't get the idea what your code is doing. Could you explain it? Furthermore what is the unregister function in general exactly doing. Am I right in thinking it stops the previous defined registering of an event or is it doing something else?

greets
Max Stephan
Eric Lemoine-2-2 wrote:
Hi

it should work. I just did the following in the FireBug console:

>>> var cb = function() {}; scope = {};
>>> var e = new OpenLayers.Events({}, null, ["eventtype"]);
>>> e.register("eventtype", scope, cb);
>>> e.listeners["eventtype"].length == 1
>>> e.unregister("eventtype", scope, cb);
>>> e.listeners["eventtype"].length == 0

Cheers,

--
Eric Lemoine
Eric Lemoine-2-2

Re: How to stop registering events

Reply Threaded More More options
Print post
Permalink
On Thu, Oct 29, 2009 at 3:17 PM, Max Stephan <[hidden email]> wrote:
>
> Hi Eric,
>
> thanks for your help but I don't get the idea what your code is doing. Could
> you explain it?

it was just to show you that unregister works as expected.

> Furthermore what is the unregister function in general
> exactly doing. Am I right in thinking it stops the previous defined
> registering of an event or is it doing something else?

it unregisters an event listener, it doesn't stop the event from being
triggered.


--
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : [hidden email]
http://www.camptocamp.com
_______________________________________________
Users mailing list
[hidden email]
http://openlayers.org/mailman/listinfo/users
Max Stephan

Re: How to stop registering events

Reply Threaded More More options
Print post
Permalink
Ok, thanks. Now the unregistering works and the function that gets called when the event was triggered only runs once. But another problem appeared.
The layer that contains the features I want to select is shown or hidden depending on the resolution. For this reason I need to query the WFS-Server if the searched feature is not in the current bounds or the layer is not shown at all because of the resolution of the map. By doing this I can get the bounds of the searched feature, zoom to it and then wait for the layer that contains features of this featuretype to load. When it has finished loading I can select the feature.

So, what´s the problem now? If the feature is already in the layer so that no extra WFS-query has to be performed selecting it works flawlessly. I can click out of this feature and it get´s unselected etc.
On the other hand, when an extra-WFS-request has to be performed the feature also gets selected and the map zooms to it. BUT: It leads to a very strange behavior:
1. when clicking outside the feature it doesn´t get unselected. It only get´s unselected when clicking on another feature
2. some features that I select after this immediately get unselected again after they´ve got marked as selected and the map zoomed to them

This behaviour persists so I guess something got corrupted in the script. It also is existent when I´m only zooming to the queried feature (without selecting it).

If needed I can give you the URL to the application via private mail but I can´t make it public.

Here´s the code segment with comments:
function zoomToTrigger(evt){
	aufrufCounter ++; //just for debugging
	document.getElementById('selectStatus').innerHTML+="zoomTrigger ausgef&uuml;hrt;<br>";
	// unregister the loadend event
	wfsLiegenschaften.events.unregister('loadend',wfsLiegenschaften,zoomToTrigger);
	//
	zoomToLiegenschaft(globalID);
};

/* Given an id this method first calls the searchFeature-method to find out if this feature
is already loaded into the layer. This is necessary because the layer is only visible in an
given resolution-interval. If that´s not the case (false returned) it calls the getFeatureBounds-method
which performs a wfs-filter-request, calculates the bounds for the feature, zooms to this extend and waits
for the layer to load and after that event was triggerd calls the zoomToTrigger-method which
unregister the loadend-event and recursively calls the zoomToLiegenschaft-method */
function zoomToLiegenschaft(id){
	// remembers the id that was searched for in a global attribute
	globalID = id;
	searchResult = searchFeature(wfsLiegenschaften,id);

	if(searchResult==false){
		getFeatureBounds('jos:Liegenschaften','FlaechenID',id);
	}
};

/* finds out if a feature with the given id exists in the layer. If it´s the case
this feature gets selected (selecting it automatically zooms to it´s bounds) and true is
returned. If the feature is not available "false" is returned. */
function searchFeature(layer,id){
	for(var feat in layer.features){
		if(layer.features[feat].attributes.FlaechenID == id){
			selectControl.unselectAll();
			selectControl.select(layer.features[feat]);
			return true;
		}
	}
	return false;
};

function getFeatureBounds(layer,filterAttribute,filterValue){
	var params = new Array();
	// Schreiben der gesuchten ID in globale Variable, damit sie für andere Methoden zur Verfügung steht
	temp_filterValue = filterValue;
	temp_layer = layer;
	params[0] = "typename="+layer;
	params[1] = "FILTER=<Filter>";
	params[1] += "<PropertyIsEqualTo>";
	params[1] += "<PropertyName>" + filterAttribute + "</PropertyName>";
	params[1] += "<Literal>" + filterValue + "</Literal>";
	params[1] += "</PropertyIsEqualTo>";
	params[1] += "</Filter>";
	try{
	OpenLayers.loadURL('//GEOSERVER-Location//',('?srs=EPSG:31468&SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&'+params[0]+'&'+params[1]),readGML,readGML,loadFailure);
	}
	catch(err){
		showError("@loadGML: Fehler beim Laden der GML (" + err.message + ")");
	}
}

function loadFailure(response){
	showError("Fehler beim Laden der GML (" + err.message +")");
}

function readGML(response){
	// Parse the GML
	gml = new OpenLayers.Format.GML();
	featureList = gml.read(response.responseText);
	// Get the Bounds for the received features
	featureBoundsList = new Array();
	for(var features in featureList){
		featureBoundsList[features] = featureList[features].geometry.getBounds();
	}
	// calculate the Bounds for the feature/features
	totalBounds = 0;
	for(var bounds in featureBoundsList){
		if(totalBounds == 0){totalBounds = featureBoundsList[bounds];}
		else{totalBounds.extend(featureBoundsList[bounds]);}
	}
	// zoom to the calculated bounds
	map.zoomToExtent(totalBounds);
	// register when the layer that contains the features finished loading and call the zoomToTrigger-method
	wfsLiegenschaften.events.register('loadend',wfsLiegenschaften,zoomToTrigger);
}

Eric Lemoine-2-2 wrote:
On Thu, Oct 29, 2009 at 3:17 PM, Max Stephan <x.zam@gmx.net> wrote:
>
> Hi Eric,
>
> thanks for your help but I don't get the idea what your code is doing. Could
> you explain it?

it was just to show you that unregister works as expected.

> Furthermore what is the unregister function in general
> exactly doing. Am I right in thinking it stops the previous defined
> registering of an event or is it doing something else?

it unregisters an event listener, it doesn't stop the event from being
triggered.


--
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : eric.lemoine@camptocamp.com
http://www.camptocamp.com
_______________________________________________
Users mailing list
Users@openlayers.org
http://openlayers.org/mailman/listinfo/users