1 | var map, layer, overlay; |
---|
2 | |
---|
3 | var projectionOptions = { |
---|
4 | 'EPSG:3574': { |
---|
5 | projection: new OpenLayers.Projection('EPSG:3574'), |
---|
6 | units: 'm', |
---|
7 | maxExtent: new OpenLayers.Bounds(-5505054, -5505054, 5505054, 5505054), |
---|
8 | maxResolution: 5505054 / 128, |
---|
9 | numZoomLevels: 18 |
---|
10 | }, |
---|
11 | 'EPSG:3576': { |
---|
12 | projection: new OpenLayers.Projection('EPSG:3576'), |
---|
13 | units: 'm', |
---|
14 | maxExtent: new OpenLayers.Bounds(-5505054, -5505054, 5505054, 5505054), |
---|
15 | maxResolution: 5505054 / 128, |
---|
16 | numZoomLevels: 18 |
---|
17 | }, |
---|
18 | 'EPSG:3571': { |
---|
19 | projection: new OpenLayers.Projection('EPSG:3571'), |
---|
20 | units: 'm', |
---|
21 | maxExtent: new OpenLayers.Bounds(-5505054, -5505054, 5505054, 5505054), |
---|
22 | maxResolution: 5505054 / 128, |
---|
23 | numZoomLevels: 18 |
---|
24 | }, |
---|
25 | 'EPSG:3573': { |
---|
26 | projection: new OpenLayers.Projection('EPSG:3573'), |
---|
27 | units: 'm', |
---|
28 | maxExtent: new OpenLayers.Bounds(-5505054, -5505054, 5505054, 5505054), |
---|
29 | maxResolution: 5505054 / 128, |
---|
30 | numZoomLevels: 18 |
---|
31 | } |
---|
32 | }; |
---|
33 | |
---|
34 | function setProjection() { |
---|
35 | projCode = this.innerHTML; |
---|
36 | var oldExtent = map.getExtent(); |
---|
37 | var oldCenter = map.getCenter(); |
---|
38 | var oldProjection = map.getProjectionObject(); |
---|
39 | |
---|
40 | // map projection is controlled by the base layer |
---|
41 | map.baseLayer.addOptions(projectionOptions[projCode]); |
---|
42 | |
---|
43 | // with the base layer updated, the map has the new projection now |
---|
44 | var newProjection = map.getProjectionObject(); |
---|
45 | |
---|
46 | // transform the center of the old projection, not the extent |
---|
47 | map.setCenter( |
---|
48 | oldCenter.transform(oldProjection, newProjection, |
---|
49 | map.getZoomForExtent(oldExtent.transform(oldProjection, newProjection)) |
---|
50 | )); |
---|
51 | |
---|
52 | for (var i=map.layers.length-1; i>=0; --i) { |
---|
53 | // update grid settings |
---|
54 | map.layers[i].addOptions(projectionOptions[projCode]); |
---|
55 | // redraw layer - just in case center and zoom are the same in old and |
---|
56 | // new projection |
---|
57 | map.layers[i].redraw(); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | function init() { |
---|
62 | map = new OpenLayers.Map('map'); |
---|
63 | layer = new OpenLayers.Layer.WMS( |
---|
64 | 'world', |
---|
65 | 'http://v2.suite.opengeo.org/geoserver/wms', |
---|
66 | {layers: 'world', version: '1.1.1'}, |
---|
67 | projectionOptions['EPSG:3574'] |
---|
68 | ); |
---|
69 | overlay = new OpenLayers.Layer.WMS( |
---|
70 | 'world', |
---|
71 | 'http://v2.suite.opengeo.org/geoserver/wms', |
---|
72 | {transparent: 'true', layers: 'world:borders', styles: 'line'}, |
---|
73 | projectionOptions['EPSG:3574'] |
---|
74 | ); |
---|
75 | overlay.isBaseLayer = false; |
---|
76 | map.addLayers([layer, overlay]); |
---|
77 | map.zoomToMaxExtent(); |
---|
78 | |
---|
79 | // add behaviour to dom elements |
---|
80 | document.getElementById('epsg3574').onclick = setProjection; |
---|
81 | document.getElementById('epsg3576').onclick = setProjection; |
---|
82 | document.getElementById('epsg3571').onclick = setProjection; |
---|
83 | document.getElementById('epsg3573').onclick = setProjection; |
---|
84 | } |
---|