1
|
function CdmOpenlayersMap(mapElement, mapserverBaseUrl, options){
|
2
|
|
3
|
var legendImgSrc = null;
|
4
|
|
5
|
var map = null;
|
6
|
|
7
|
var layerByNameMap = {
|
8
|
tdwg1: 'topp:tdwg_level_1',
|
9
|
tdwg2: 'topp:tdwg_level_2',
|
10
|
tdwg3: 'topp:tdwg_level_3',
|
11
|
tdwg4: 'topp:tdwg_level_4'
|
12
|
};
|
13
|
|
14
|
var defaultLayerOptions = {
|
15
|
maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90),
|
16
|
isBaseLayer: false,
|
17
|
displayInLayerSwitcher: false
|
18
|
};
|
19
|
|
20
|
var mapOptions={
|
21
|
controls:
|
22
|
[
|
23
|
new OpenLayers.Control.PanZoom(),
|
24
|
// new OpenLayers.Control.LayerSwitcher(),
|
25
|
new OpenLayers.Control.Navigation({zoomWheelEnabled: false, handleRightClicks:true, zoomBoxKeyMask: OpenLayers.Handler.MOD_CTRL})
|
26
|
],
|
27
|
maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90),
|
28
|
maxResolution: (360 / options.displayWidth),
|
29
|
restrictedExtent: new OpenLayers.Bounds(-180, -90, 180, 90),
|
30
|
projection: new OpenLayers.Projection("EPSG:4326")
|
31
|
};
|
32
|
|
33
|
|
34
|
var baseLayer = new OpenLayers.Layer.WMS(
|
35
|
"OpenLayers WMS",
|
36
|
"http://labs.metacarta.com/wms/vmap0",
|
37
|
{layers: 'basic'},
|
38
|
this.defaultLayerOptions );
|
39
|
|
40
|
baseLayer.options.isBaseLayer = true;
|
41
|
|
42
|
/**
|
43
|
*
|
44
|
*/
|
45
|
this.init = function(){
|
46
|
|
47
|
// get and prepare the Urls
|
48
|
var distributionQuery = mapElement.attr('distributionQuery');
|
49
|
if(typeof legendPosition == 'number'){
|
50
|
distributionQuery = mergeQueryStrings(distributionQuery, 'legend=1&mlp=' + options.legendPosition);
|
51
|
}
|
52
|
// jsonp
|
53
|
distributionQuery = mergeQueryStrings(distributionQuery, 'foo=foo');
|
54
|
|
55
|
var legendFormatQuery = mapElement.attr('legendFormatQuery');
|
56
|
if(legendFormatQuery !== undefined){
|
57
|
legendImgSrc = mergeQueryStrings('/GetLegendGraphic?SERVICE=WMS&VERSION=1.1.1', legendFormatQuery);
|
58
|
}
|
59
|
|
60
|
var mapServiceRequest = mapserverBaseUrl + '/areas.php?' + distributionQuery;
|
61
|
$.ajax({
|
62
|
url: mapServiceRequest,
|
63
|
dataType: "jsonp",
|
64
|
success: function(data){
|
65
|
initOpenLayers(data);
|
66
|
},
|
67
|
jsonp: "foo"
|
68
|
});
|
69
|
};
|
70
|
|
71
|
/**
|
72
|
*
|
73
|
*/
|
74
|
var initOpenLayers = function(mapResponseObj){
|
75
|
|
76
|
// instatiate the openlayers viewer
|
77
|
map = new OpenLayers.Map('openlayers_map', mapOptions);
|
78
|
|
79
|
//add the base layer
|
80
|
map.addLayers([baseLayer]);
|
81
|
|
82
|
// add additional layers, get them from
|
83
|
// the mapResponseObj
|
84
|
if(mapResponseObj !== undefined){
|
85
|
for ( var i in mapResponseObj.layers) {
|
86
|
|
87
|
var layerData = mapResponseObj.layers[i];
|
88
|
|
89
|
var layer = new OpenLayers.Layer.WMS.Untiled(
|
90
|
layerData.tdwg,
|
91
|
mapResponseObj.geoserver + "/wms",
|
92
|
{layers: layerByNameMap[layerData.tdwg] ,transparent:"true", format:"image/png"},
|
93
|
defaultLayerOptions );
|
94
|
|
95
|
layer.params.SLD = layerData.sld;
|
96
|
map.addLayers([layer]);
|
97
|
|
98
|
}
|
99
|
|
100
|
map.addControl(new OpenLayers.Control.LayerSwitcher({'ascending':false}));
|
101
|
|
102
|
|
103
|
// zoom to the required area
|
104
|
var boundsStr = (options.boundingBox !== undefined ? options.boundingBox : (mapResponseObj.bbox ? mapResponseObj.bbox : '-180, -90, 180, 90') );
|
105
|
var zoomToBounds = OpenLayers.Bounds.fromString( boundsStr );
|
106
|
map.zoomToExtent(zoomToBounds, false);
|
107
|
|
108
|
// HACK:
|
109
|
var hack = "/var/www/";
|
110
|
mapResponseObj.legend = "http://edit.br.fgov.be/" + mapResponseObj.legend.substr(hack.length - 1);
|
111
|
// END OF HACK
|
112
|
if(options.legendPosition !== undefined){
|
113
|
var legendSrcUrl = mapResponseObj.geoserver + legendImgSrc + mapResponseObj.legend;
|
114
|
addLegendAsElement(legendSrcUrl);
|
115
|
//addLegendAsLayer(legendSrcUrl, map);
|
116
|
}
|
117
|
}
|
118
|
|
119
|
|
120
|
};
|
121
|
|
122
|
var addLegendAsElement= function(legendSrcUrl){
|
123
|
mapElement.after('<div class="openlayers_legend"><img src="' + legendSrcUrl + '"></div>');
|
124
|
mapElement.next('.openlayers_legend').find('img').load(function () {
|
125
|
$(this).parent()
|
126
|
.css('position', 'relative')
|
127
|
.css('z-index', '30000')
|
128
|
.css('top', -mapElement.height())
|
129
|
.css('left', mapElement.width()- $(this).width());
|
130
|
});
|
131
|
};
|
132
|
|
133
|
|
134
|
var addLegendAsLayer= function(legendSrcUrl, map){
|
135
|
var w, h;
|
136
|
|
137
|
// 1. download imge to find height and width
|
138
|
mapElement.after('<div class="openlayers_legend"><img src="' + legendSrcUrl + '"></div>');
|
139
|
mapElement.next('.openlayers_legend').css('display', 'none').find('img').load(function () {
|
140
|
|
141
|
w = mapElement.next('.openlayers_legend').find('img').width();
|
142
|
h = mapElement.next('.openlayers_legend').find('img').height();
|
143
|
mapElement.next('.openlayers_legend').remove();
|
144
|
|
145
|
createLegendLayer();
|
146
|
});
|
147
|
|
148
|
// 2. create the Legend Layer
|
149
|
var createLegendLayer = function(){
|
150
|
|
151
|
|
152
|
var legendLayerOptions={
|
153
|
maxResolution: '.$maxRes.',
|
154
|
maxExtent: new OpenLayers.Bounds(0, 0, w, h)
|
155
|
};
|
156
|
|
157
|
var legendLayer = new OpenLayers.Layer.Image(
|
158
|
'Legend',
|
159
|
legendSrcUrl,
|
160
|
new OpenLayers.Bounds(0, 0, w, h),
|
161
|
new OpenLayers.Size(w, h),
|
162
|
imageLayerOptions
|
163
|
);
|
164
|
}
|
165
|
|
166
|
};
|
167
|
|
168
|
/**
|
169
|
* merge 2 Url query strings
|
170
|
*/
|
171
|
var mergeQueryStrings = function(queryStr1, queryStr2){
|
172
|
if(queryStr1.charAt(queryStr1.length - 1) != '&'){
|
173
|
queryStr1 += '&';
|
174
|
}
|
175
|
if(queryStr2.charAt(0) == '&'){
|
176
|
return queryStr1 + queryStr2.substr(1);
|
177
|
} else {
|
178
|
return queryStr1 + queryStr2;
|
179
|
}
|
180
|
};
|
181
|
|
182
|
}
|
183
|
|
184
|
|
185
|
(function($){
|
186
|
|
187
|
$.fn.cdm_openlayers_map = function(mapserverBaseUrl, options) {
|
188
|
|
189
|
var opts = $.extend({},$.fn.cdm_openlayers_map.defaults, options);
|
190
|
|
191
|
return this.each(function(){
|
192
|
|
193
|
var openlayers_map = new CdmOpenlayersMap($(this), mapserverBaseUrl, opts);
|
194
|
openlayers_map.init();
|
195
|
|
196
|
}); // END each
|
197
|
|
198
|
}; // END cdm_openlayers_map
|
199
|
|
200
|
})(jQuery);
|
201
|
|
202
|
|
203
|
$.fn.cdm_openlayers_map.defaults = { // set up default options
|
204
|
legendPosition: null, // 1,2,3,4,5,6 = display a legend in the corner specified by the number
|
205
|
displayWidth: 400,
|
206
|
boundingBox: null
|
207
|
};
|
208
|
|