1 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
4 OpenLayers.Control.SypAttribution = OpenLayers.Class (OpenLayers.Control.Attribution, {
5 updateAttribution: function() {
6 var attributions = [SypStrings.propulsedByLink];
7 if (this.map && this.map.layers) {
8 for(var i=0, len=this.map.layers.length; i<len; i++) {
9 var layer = this.map.layers[i];
10 if (layer.attribution && layer.getVisibility()) {
11 attributions.push( layer.attribution );
14 this.div.innerHTML = attributions.join(this.separator);
21 MARKER_ICON: "openlayers/img/marker-blue.png",
22 MARKER_ICON_HEIGHT: 25,
23 MARKER_SELECT_ICON: "openlayers/img/marker-green.png",
24 MARKER_SELECT_ICON_HEIGHT: 25
33 this.map = new OpenLayers.Map("map", {
35 new OpenLayers.Control.Navigation(),
36 new OpenLayers.Control.PanZoom(),
37 new OpenLayers.Control.Permalink(),
38 new OpenLayers.Control.SypAttribution()
40 projection: new OpenLayers.Projection("EPSG:900913"),
41 displayProjection: new OpenLayers.Projection("EPSG:4326")
44 this.baseLayer = this.createBaseLayer();
45 this.dataLayer = this.createDataLayer();
46 this.map.addLayers([this.baseLayer, this.dataLayer]);
48 this.selectControl = this.createSelectControl();
49 this.map.addControl(this.selectControl);
50 this.selectControl.activate();
52 if (!this.map.getCenter()) {
53 var centerBounds = new OpenLayers.Bounds();
55 var mapProj = this.map.getProjectionObject();
56 var sypOrigProj = new OpenLayers.Projection("EPSG:4326");
58 var bottomLeft = new OpenLayers.LonLat(sypOrig[0],sypOrig[1]);
59 bottomLeft = bottomLeft.transform(sypOrigProj, mapProj);
60 var topRight = new OpenLayers.LonLat(sypOrig[2],sypOrig[3])
61 topRight = topRight.transform(sypOrigProj, mapProj);
63 centerBounds.extend(bottomLeft);
64 centerBounds.extend(topRight);
65 this.map.zoomToExtent(centerBounds);
69 createBaseLayer: function() {
70 return new OpenLayers.Layer.OSM("OSM");
73 createDataLayer: function(map) {
74 var styleMap = new OpenLayers.StyleMap (
76 externalGraphic: this.Settings.MARKER_ICON,
77 graphicHeight: this.Settings.MARKER_ICON_HEIGHT
81 externalGraphic: this.Settings.MARKER_SELECT_ICON,
82 graphicHeight: this.Settings.MARKER_SELECT_ICON_HEIGHT
87 var layer = new OpenLayers.Layer.GML("KML", "items.php",
90 format: OpenLayers.Format.KML,
91 projection: this.map.displayProjection,
92 eventListeners: { scope: this,
93 loadend: this.checkForFeatures
100 createSelectControl: function() {
101 var control = new OpenLayers.Control.SelectFeature(
103 onSelect: this.onFeatureSelect,
104 onUnselect: this.onFeatureUnselect,
111 checkForFeatures: function() {
112 var features = this.dataLayer.features;
113 if (features.length == 0) {
114 var message = SypStrings.noImageRegistered;
115 this.Utils.displayUserMessage(message, "warn");
119 createPopup: function(position, contentHTML) {
120 var popup = new OpenLayers.Popup.Anchored("popup",
126 popup.autoSize = true;
127 popup.backgroundColor = ""; // deal with it in css
128 popup.border = ""; // deal with it in css
129 popup.closeOnMove = true;
133 onFeatureUnselect: function (feature) {
134 var map = feature.layer.map;
135 var permaControl = map.getControlsByClass("OpenLayers.Control.Permalink");
136 if (permaControl[0]) {
137 permaControl[0].div.style.display = "";
139 if (!feature.popup) {
140 this.map.events.unregister("movestart", this, this._unselect);
143 popup = feature.popup;
144 if (popup.visible()) {
149 onFeatureSelect: function(feature) {
150 var map = feature.layer.map;
151 var permaControl = map.getControlsByClass("OpenLayers.Control.Permalink");
152 if (permaControl[0]) {
153 permaControl[0].div.style.display = "none";
155 var popup = feature.popup;
157 var brCorner = SYP.Utils.brCorner(map, 8);
159 // we cannot reuse popup; we need to recreate it in order for IE
160 // expressions to work. Otherwise, we get a 0x0 image on second view.
165 if (feature.attributes.name) {
166 contentHTML = "<h2>" +
167 feature.attributes.name +
169 feature.attributes.description;
171 contentHTML = feature.attributes.description;
173 if (!contentHTML || !contentHTML.length) {
174 this.map.events.register("movestart", this, this._unselect = function () { this.unselect(feature)});
177 popup = SYP.createPopup(brCorner, contentHTML);
179 popup.hide = function () {
180 OpenLayers.Element.hide(this.div);
181 control.unselect(feature);
184 feature.popup = popup;
188 brCorner: function(map, margin) {
189 var bounds = map.calculateBounds();
190 var corner = new OpenLayers.LonLat(bounds.right, bounds.bottom);
191 var cornerAsPx = map.getPixelFromLonLat(corner);
192 cornerAsPx = cornerAsPx.add( -margin, -margin);
193 corner = map.getLonLatFromPixel(cornerAsPx);
196 displayUserMessage: function(message, status) {
197 var div = document.getElementById('message');
198 while (div.firstChild)
199 div.removeChild(div.firstChild);
200 var textNode = document.createTextNode(message);
203 div.style.color = "red";
206 div.style.color = "#FF8C00";
209 div.style.color = "green";
212 div.style.color = "black";
215 div.style.display = "block";
216 div.appendChild(textNode);
221 // if possible, determine language with HTTP_ACCEPT_LANGUAGE instead of
222 // navigator.language
223 if (OpenLayers.Lang[SypStrings.language]) {
224 OpenLayers.Lang.setCode(SypStrings.language);
228 OpenLayers.Console.userError = function(error) {
229 SYP.Utils.displayUserMessage(error, "error");