OpenLayers OpenLayers

Ticket #2140 (closed bug: invalid)

Opened 9 months ago

Last modified 9 months ago

callback click does'nt work after a click on the NavToolBar

Reported by: jclb Assigned to: tschaub
Priority: major Milestone: 2.9 Release
Component: Control.SelectFeature Version: 2.8 RC4
Keywords: callback click SelectFeature Cc:
State: Complete

Description

From 2.8 RC6

After a free zoom the the NavToolBar, I lose the click over all my features.

There is no JS error.

var map; var fichier_kml;

function selectionnerZone(lon, lat, zoom) {

var defaultLonglat = new OpenLayers.LonLat(lon, lat); var wgs84 = new OpenLayers.Projection("EPSG:4326"); var merc = new OpenLayers.Projection("EPSG:900913"); defaultLonglat.transform(wgs84, merc); map.setCenter(defaultLonglat, zoom);

}

function doClick(feature) {

selectedFeature = feature; vis=[]; if (feature.attributes.description.substr(0,3)=='new') {

tab = eval(feature.attributes.description) afficherCommune(tab, true) selectControl.unselect(selectedFeature);

} else {

afficherVille(new Array(feature.attributes.name,feature.attributes.description),true) selectControl.unselect(selectedFeature);

}

}

// fonction pour forcer le cache sur les fichiers kml function code_random(){

return Math.round(Math.random()*1000000);

}

// avoid pink tiles OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3; OpenLayers.Util.onImageLoadErrorColor = "transparent";

// gestion des tuiles avec un serveur distant openstreet function osm_getTileURL(bounds) {

var res = this.map.getResolution(); var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w)); var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h)); var z = this.map.getZoom(); var limit = Math.pow(2, z);

if (y < 0 y >= limit) {

return OpenLayers.Util.getImagesLocation() + "404.png";

} else {

x = ((x % limit) + limit) % limit; return this.url + z + "/" + x + "/" + y + "." + this.type;

}

}

// TUILES JPG function get_my_url (bounds) {

var res = this.map.getResolution(); var x = Math.round ((bounds.left - this.maxExtent.left) / (res * this.tileSize.w)); var y = Math.round ((this.maxExtent.top - bounds.top) / (res * this.tileSize.h)); var z = this.map.getZoom();

var path = z + "/row" + y + "/" + z + "_" + x + "-" + y + ".jpg"; var url = this.url; if (url instanceof Array) {

url = this.selectUrl(path, url);

} return url + path;

}

// fonction pour gerer les tuiles des rivieres function get_my_url_gif (bounds) {

var res = this.map.getResolution(); var x = Math.round ((bounds.left - this.maxExtent.left) / (res * this.tileSize.w)); var y = Math.round ((this.maxExtent.top - bounds.top) / (res * this.tileSize.h)); var z = this.map.getZoom();

var path = z + "/row" + y + "/" + z + "_" + x + "-" + y + ".gif"; var url = this.url; if (url instanceof Array) {

url = this.selectUrl(path, url);

}

return url + path;

}

// Initialisation function init(lon, lat, zoom, div){

PanBar = new OpenLayers.Control.PanZoom() PanBar.zoomWorldIcon = true

// Remplacement de l'évènement double click par défaut qui fait un zoom par la signalisation d'un phénomène NavControl = new OpenLayers.Control.NavToolbar() NavControl.controls[0].defaultDblClick = doubleClick;

var options = {

projection: new OpenLayers.Projection("EPSG:900913"), displayProjection: new OpenLayers.Projection("EPSG:4326"), units: "m", numZoomLevels: 11, maxResolution: 156543.0339, maxExtent: new OpenLayers.Bounds(-20037508, -20037508,20037508, 20037508.34), controls: [PanBar, NavControl]

}

function doubleClick(evt) {

lonLat = map.getLonLatFromPixel(evt.xy);

if (map.displayProjection) {

lonLat.transform(map.getProjectionObject(),

map.displayProjection );

}

//alert('lon='+lonLat.lon+' lat='+lonLat.lat+' zoom='+map.getZoom())

if (controlerLonLat(lonLat.lon,lonLat.lat)) {

if (confirm("Voulez-vous signaler un phénomène à ces coordonnées ?")) {

window.open('sys_choixPheno.php?lon='+lonLat.lon+'&lat='+lonLat.lat,'_self')

}

}

}

map = new OpenLayers.Map(div, options); var mp = new OpenLayers.Control.MousePosition();

map.addControl(mp);

// creation du fond carte_relief donnees en local var carte_relief = new OpenLayers.Layer.TMS("Relief", "../../relief/z", { 'getURL':get_my_url } ,{'maxZoomLevel':11,'buffer':1,'maxExtent': new OpenLayers.Bounds(-180,-90,180,90), 'maxResolution': "auto"} ); carte_relief.setIsBaseLayer(true);

// creation de la couche Observations var observations = new OpenLayers.Layer.GML(

"Observations", "../kml/obs"+fichier_kml+".kml" , {projection: new OpenLayers.Projection("EPSG:4326"), formatOptions: {extractAttributes: true, extractStyles: true, maxDepth:1}, format: OpenLayers.Format.KML}

)

// creation de la couche Communes var communes = new OpenLayers.Layer.GML(

"Communes", "../kml/commune10000.kml" , {projection: new OpenLayers.Projection("EPSG:4326"), formatOptions: {extractAttributes: true, extractStyles: true, maxDepth:1}, format: OpenLayers.Format.KML}

)

// creation de la couche hydro

var water = new OpenLayers.Layer.TMS( "Fleuves-rivières","../../water/z",{ 'getURL':get_my_url_gif} ,{'maxZoomLevel':9});

water.setIsBaseLayer(false); water.setVisibility(false);

// creation de la couche admin var admin = new OpenLayers.Layer.TMS(

"Régions-Agglomerations", "../../admin/z", { 'getURL':get_my_url_gif}, {'maxZoomLevel':11}

); admin.setIsBaseLayer(false); admin.setVisibility(false);

// Ajout des couches map.addLayers([carte_relief,communes,observations,water,admin]); map.zoomToMaxExtent();

// ajout des choix des couches

map.addControl(new OpenLayers.Control.LayerSwitcher());

// on ajoute l echelle var scale = new OpenLayers.Control.ScaleBar(); map.addControl(scale);

// initialise la carte centrage et zoom var wgs84 = new OpenLayers.Projection("EPSG:4326"); var merc = new OpenLayers.Projection("EPSG:900913"); var defaultLat = lat; var defaultLong = lon; var defaultLonglat = new OpenLayers.LonLat(defaultLong, defaultLat); defaultLonglat.transform(wgs84, merc); var defaultZoom = zoom; map.setCenter(defaultLonglat, defaultZoom);

// permet de faire glisser la carte a la souris selectControl = new OpenLayers.Control.SelectFeature(

new Array(map.layers[1],map.layers[2]) , {callbacks: {'click':doClick}}

) map.addControl(selectControl); selectControl.activate();

}

Attachments

code.zip (28.3 kB) - added by jclb on 06/18/09 04:09:48.
code
js.zip (2.3 kB) - added by jclb on 06/18/09 08:47:57.
js2.zip (2.3 kB) - added by jclb on 06/18/09 09:53:17.
navtoolbar-selectfeature.html (1.9 kB) - added by ahocevar on 06/18/09 09:54:01.
navtoolbar with properly configured selectfeature control

Change History

06/17/09 10:58:09 changed by ahocevar

  • state set to Awaiting User Feedback.
  • type changed from feature to bug.
  • milestone changed from 2.8 Release to 2.9 Release.

jclb: The issue is not a regression, because it uses the SelectFeature control with multiple layers, which is a new feature in 2.8.

Looking at the code (which would be easier readable if provided as attachment), I see at least one thing that cannot work: you assign

NavControl.controls[0].defaultDblClick = doubleClick;

before defining the doubleClick callback.

To investigate the issue, you would at least have to provide everything needed to run your code. Either as a publicly accessable url, or with all required files in a zip. This will, however, still not make investigating it a fun experience. The best option would be to create a stripped down example showing your issue, preferrably based on one of the OpenLayers examples, with just the pieces of code added that make it not work.

06/18/09 04:09:48 changed by jclb

  • attachment code.zip added.

code

06/18/09 04:14:21 changed by jclb

Hello,

Sorry for the code in the message. I didn't found how to attach the file. You can try the application on this beta URL http://ns202439.ovh.net/VDT3. I can't send you all the code, it's principally PHP code whith many librairies, PEAR, SMARTY... I generate dynamic KML files for the data with a special KML for style (see sample of KML files in attachement)

If it's necessary I could do a static sampler of the application, but not yet, to much work to do.

Thank's for your help

06/18/09 06:30:38 changed by ahocevar

  • status changed from new to closed.
  • resolution set to invalid.

The issue reported here is an application issue: the NavToolbar control is a control panel, and its controls claim the same events as the SelectFeature control. This means that the SelectFeature control needs to be deactivated when activating the ZoomBox control, and vice versa. The easiest way to do this is probably adding the SelectFeature control to the NavToolbar panel (i.e. NavControl.addControls([selectControl]) instead of map.addControl(selectControl)). The selectControl must also be initialized with the "type: OpenLayers.Control.TYPE_TOOL" option to make that work correctly. Finally, to have the selectControl active by default, you have to say NavControl.activateControl(selectControl) after adding it.

06/18/09 06:31:44 changed by ahocevar

jclb: If you need more help, please ask on the users list.

06/18/09 08:47:57 changed by jclb

  • attachment js.zip added.

06/18/09 08:48:28 changed by jclb

It works very with OpenLayers 2.7, you can see it at this URL : http://www.veilleurs-du-temp.fr I've just done a correction in KML.js for the URL style who works in 2.8 RC6. The only difference in the kml.js file is that I work with a single layer instead of an array of layers. I've tried 2.8 RC6 with a single layer, same code, and it doesn't work.

I've tried your solution see js.zip without succes.

Does it means that there's no compatibilty between 2.7 and 2.8 ?

06/18/09 08:49:20 changed by jclb

  • status changed from closed to reopened.
  • state changed from Awaiting User Feedback to Needs Discussion.
  • resolution deleted.

06/18/09 09:26:56 changed by ahocevar

  • state changed from Needs Discussion to Awaiting User Feedback.

jclb: the kml.js files of both applications show more differences, so it is impossible to investigate this further. If you can provide a simplified example (as explained in my first comment), we can have another look at this.

06/18/09 09:53:17 changed by jclb

  • attachment js2.zip added.

06/18/09 09:53:39 changed by jclb

Hi,

I send you a new kml.js (js2.zip) with what you asked me exactly.

Doesn't work better.

Here's the diff between the two olders files :

< function doClick(feature) { ---

function doClick(feature) {

182,183c182,186 < new Array(map.layers[1],map.layers[2]) , < {callbacks: {'click':doClick}} ---

//new Array(map.layers[1],map.layers[2]) , map.layers[2], {callbacks: {'click':doClick} ,type: OpenLayers.Control.TYPE_TOOL }

185,186c188,191 < map.addControl(selectControl); < selectControl.activate(); ---

//map.addControl(selectControl); //selectControl.activate(); NavControl.addControls([selectControl]) NavControl.activateControl(selectControl)

Instead of a array of layers, I've put one layer -> new Array(map.layers[1],map.layers[2]) , < {callbacks: {'click':doClick}}

//new Array(map.layers[1],map.layers[2]) , map.layers[2],

I've haded the type in the selectControl as you asked me->

,type: OpenLayers.Control.TYPE_TOOL

And I've put the select control in the NavControl instead of the map -> < map.addControl(selectControl); < selectControl.activate(); ---

//map.addControl(selectControl); //selectControl.activate(); NavControl.addControls([selectControl]) NavControl.activateControl(selectControl)

Thank's

06/18/09 09:54:01 changed by ahocevar

  • attachment navtoolbar-selectfeature.html added.

navtoolbar with properly configured selectfeature control

06/18/09 10:01:24 changed by ahocevar

  • status changed from reopened to closed.
  • resolution set to invalid.

jclb: see my attached example. It shows you how to configure the control properly.

With regards to this ticket: without the activate/deactivate handlers (lines 31-37 in the attached example), we see that with OL 2.7, selecting features still works when turning the navigation back on. But clickout does not. With OL 2.8, neither selecting nor clickout works. In either case, the right way to use selectfeature with navtoolbar is shown in the attachment, and doing it without activate/deactivate handlers for the navigation control will result in weird behavior with 2.7 as well with 2.8. This means that the ticket can be closed, the issue comes from leaving the SelectFeature control activated when activating/deactivating the ZoomBox control, which is not good practice (why? with the ZoomBox control active, zoom in by clicking on the feature. it will be selected. was that intended?.

06/19/09 01:14:10 changed by jclb

  • state changed from Awaiting User Feedback to Complete.

Hi,

Many thank's for your help. It works very well. It's not really intuitive but It works.

Thank's again !