[OpenLayers-Users] How to stop registering events

Max Stephan x.zam at gmx.net
Fri Oct 30 07:30:20 EDT 2009


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 at 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 at camptocamp.com
> http://www.camptocamp.com
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
> 
> 

-- 
View this message in context: http://n2.nabble.com/How-to-stop-registering-events-tp3911670p3918351.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.



More information about the Users mailing list