Thursday 21 June 2018

javascript - OpenLayers.Class with OpenLayers.Control.GetFeature


I'm wondering if someone could point me in the right direction to force the BBOX of OpenLayers.Control.GetFeature to be always in long/lat coordinates. I'm trying to get this with OpenLayers.Class, so the new control will inherit OpenLayers.Control.GetFeature with just the small modification in BBOX. This is what I've done:


OpenLayers.Control.myGetFeature = OpenLayers.Class(OpenLayers.Control.GetFeature,{
selectBox: function(position){
var opts = OpenLayers.Control.GetFeature(position);
var baseSRSutm = this.map.getprojectionObject();
var layerSRSdd = new OpenLayers.Projection('EPSG:4326');
opts.params.bbox = this.map.getExtent().transform(baseSRSutm,layerSRSdd).toBBOX(null,firstLayer.reverseAxisOrder());

}
return opts;
});

but I'm getting that some "}" are missing. Is this the right way to use OpenLayers.Class to do what I want?


Any help is much appreciated.


PS. I'm using OpenLayers 2.11


EDIT:


Moving "return opts;" inside the function solved the above problem but it complained about CLASS_NAME is missing. Adding that, it still complained about the same, but after a couple of clicks or boxes it sent the request, however the sent bbox is still in UTM, where could the problem be? here it is the code:


var mycontrol = function(){

var protocol = new OpenLayers.Protocol.HTTP({
url: 'http://www.....',
format: new OpenLayers.Format.GeoJSON({
ignoreExtraDims: true,
'internalProjection': new OpenLayers.Projection('EPSG:900913'),
'externalProjection': new OpenLayers.Projection('EPSG:4326')
})
});

OpenLayers.Control.myGetFeature = OpenLayers.Class(OpenLayers.Control.GetFeature,{

selectBox: function(position){
var opts = OpenLayers.Control.GetFeature(position);
var baseSRSutm = this.map.getprojectionObject();
console.log('this is the baseSRSutm' + baseSRSutm); // doesnt appear in firebug
var layerSRSdd = new OpenLayers.Projection('EPSG:4326');
opts.params.bbox = this.map.getExtent().transform(baseSRSutm,layerSRSdd).toBBOX(null,firstLayer.reverseAxisOrder());
console.log('this is the opts.params.bbox' + opts.params.bbox); // doesnt appear in firebug
return opts;
},
CLASS_NAME: "OpenLayers.Control.myGetFeature"

});

return new OpenLayers.Control.myGetFeature({
protocol: protocol,
box: true,
click: true,
single: false,
clickTolerance: 10,
eventListeners:{
// some stuff

}
});
};

because the console.log doesn't appear in firebug, I think the problem is in the OpenLayers.Control.myGetFeature and OpenLayers.Class. It seems to me that the "selectBox" function is the one I need, but the subclass itself is not read by the return.



Answer



finally it worked, thanks a lot for your suggestion Mingfeng, the trick is to use the prototype and selectBox and then apply the conversion between the base map projection and the displayProjection, in this case between 900913 and 4326. I got the idea here: http://osgeo-org.1560.n6.nabble.com/Order-Priority-of-Control-GetFeature-and-Control-WMSGetFeatureInfo-on-click-td4997282.html


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...