X-Git-Url: https://dev.renevier.net/gitweb.cgi?p=syp.git;a=blobdiff_plain;f=js%2Fsyp.js;h=40735c791cbb516ea273c0fe40a0e85d58ecb28b;hp=d8a54b5d5a59611d44cb4663e5dbec309161abb7;hb=3b38ca36fc18d34999073625a9c66dc2f05747a3;hpb=e57c166b46cf9c5961bca1b2241783944277cef5 diff --git a/js/syp.js b/js/syp.js index d8a54b5..40735c7 100644 --- a/js/syp.js +++ b/js/syp.js @@ -17,11 +17,10 @@ OpenLayers.Control.SypAttribution = OpenLayers.Class (OpenLayers.Control.Attribu }); var SYP = { - Settings: { - MARKER_ICON: "openlayers/img/marker-blue.png", - MARKER_ICON_HEIGHT: 25, - MARKER_SELECT_ICON: "openlayers/img/marker-green.png", - MARKER_SELECT_ICON_HEIGHT: 25 + Markers: { + ICON: "media/marker-normal.png", + SELECT_ICON: "media/marker-selected.png", + HEIGHT: 25 }, map: null, @@ -50,19 +49,7 @@ var SYP = { this.selectControl.activate(); if (!this.map.getCenter()) { - var centerBounds = new OpenLayers.Bounds(); - - var mapProj = this.map.getProjectionObject(); - var sypOrigProj = new OpenLayers.Projection("EPSG:4326"); - - var bottomLeft = new OpenLayers.LonLat(sypOrig[0],sypOrig[1]); - bottomLeft = bottomLeft.transform(sypOrigProj, mapProj); - var topRight = new OpenLayers.LonLat(sypOrig[2],sypOrig[3]) - topRight = topRight.transform(sypOrigProj, mapProj); - - centerBounds.extend(bottomLeft); - centerBounds.extend(topRight); - this.map.zoomToExtent(centerBounds); + this.map.setCenter(new OpenLayers.LonLat(0, 0), 0); } }, @@ -72,20 +59,20 @@ var SYP = { createDataLayer: function(map) { var defaultStyle = new OpenLayers.Style({ - externalGraphic: this.Settings.MARKER_ICON, + externalGraphic: this.Markers.ICON, graphicHeight: "${height}" }, { context: { height: function(feature) { - var defaultHeight = SYP.Settings.MARKER_ICON_HEIGHT || 32; + var defaultHeight = SYP.Markers.HEIGHT || 32; var increase = 4 * (feature.attributes.count - 1); return Math.min(defaultHeight + increase, 50); } } }); var selectStyle = new OpenLayers.Style({ - externalGraphic: this.Settings.MARKER_SELECT_ICON, - graphicHeight: this.Settings.MARKER_SELECT_ICON_HEIGHT || 32 + externalGraphic: this.Markers.SELECT_ICON, + graphicHeight: this.Markers.HEIGHT || 32 }); var styleMap = new OpenLayers.StyleMap ( {"default": defaultStyle, @@ -100,7 +87,7 @@ var SYP = { format: OpenLayers.Format.KML, projection: this.map.displayProjection, eventListeners: { scope: this, - loadend: this.checkForFeatures + loadend: this.dataLayerEndLoad } }); @@ -118,12 +105,38 @@ var SYP = { return control; }, + dataLayerEndLoad: function() { + if (!this.checkForFeatures()) { + return; + } + + var map = this.map; + if (map.getControlsByClass("OpenLayers.Control.ArgParser")[0].lat + == undefined) { // map center was not set in ArgParser control. + var orig = this.Utils.mbr (this.dataLayer); + var centerBounds = new OpenLayers.Bounds(); + + var mapProj = map.getProjectionObject(); + var sypOrigProj = new OpenLayers.Projection("EPSG:4326"); + + var bottomLeft = new OpenLayers.LonLat(orig[0],orig[1]); + bottomLeft = bottomLeft.transform(sypOrigProj, mapProj); + var topRight = new OpenLayers.LonLat(orig[2],orig[3]) + topRight = topRight.transform(sypOrigProj, mapProj); + + centerBounds.extend(bottomLeft); + centerBounds.extend(topRight); + map.zoomToExtent(centerBounds); + } + }, + checkForFeatures: function() { var features = this.dataLayer.features; if (features.length == 0) { var message = SypStrings.noImageRegistered; this.Utils.displayUserMessage(message, "warn"); } + return !!features.length; }, createPopup: function(position, contentHTML) { @@ -170,7 +183,12 @@ var SYP = { } var popup = feature.popup; - var brCorner = SYP.Utils.brCorner(map, 8); + var popupPos = null; + if (sypSettings.popupNearfeature) { + popupPos = feature.geometry.getBounds().getCenterLonLat(); + } else { + popupPos = SYP.Utils.brCorner(map, 8); + } // we cannot reuse popup; we need to recreate it in order for IE // expressions to work. Otherwise, we get a 0x0 image on second view. @@ -190,7 +208,7 @@ var SYP = { this.map.events.register("movestart", this, this._unselect = function () { this.unselect(feature)}); return; } - popup = SYP.createPopup(brCorner, contentHTML); + popup = SYP.createPopup(popupPos, contentHTML); var control = this; popup.hide = function () { OpenLayers.Element.hide(this.div); @@ -253,6 +271,59 @@ var SYP = { corner = map.getLonLatFromPixel(cornerAsPx); return corner; }, + + /* minimum bounds rectangle containing all feature locations. + * FIXME: if two features are close, but separated by 180th meridian, + * their mbr will span the whole earth. Actually, 179° lon and -170° + * lon are considerated very near. + */ + mbr: function (layer) { + var features = []; + var map = layer.map; + + var mapProj = map.getProjectionObject(); + var sypOrigProj = new OpenLayers.Projection("EPSG:4326"); + + for (var i =0; i < layer.features.length; i++) { + if (layer.features[i].cluster) { + features = features.concat(layer.features[i].cluster); + } else { + features = features.concat(layer.features); + } + } + + var minlon = 180; + var minlat = 88; + var maxlon = -180; + var maxlat = -88; + + if (features.length == 0) { + // keep default values + } else if (features.length == 1) { + // in case there's only one feature, we show an area of at least + // 4 x 4 degrees + var pos = features[0].geometry.getBounds().getCenterLonLat().clone(); + var lonlat = pos.transform(mapProj, sypOrigProj); + + minlon = Math.max (lonlat.lon - 2, -180); + maxlon = Math.min (lonlat.lon + 2, 180); + minlat = Math.max (lonlat.lat - 2, -90); + maxlat = Math.min (lonlat.lat + 2, 90); + } else { + for (var i = 0; i < features.length; i++) { + var pos = features[i].geometry.getBounds().getCenterLonLat().clone(); + var lonlat = pos.transform(mapProj, sypOrigProj); + minlon = Math.min (lonlat.lon, minlon); + minlat = Math.min (lonlat.lat, minlat); + maxlon = Math.max (lonlat.lon, maxlon); + maxlat = Math.max (lonlat.lat, maxlat); + } + } + + return [minlon, minlat, maxlon, maxlat]; + + }, + displayUserMessage: function(message, status) { var div = document.getElementById('message'); while (div.firstChild)