1 /* Copyright (c) 2011 by MapQuery Contributors (see AUTHORS for
2 * full list of contributors). Published under the MIT license.
3 * See https://github.com/mapquery/mapquery/blob/master/LICENSE for the
4 * full text of the license. */
7 #jquery.mapquery.legend.js
8 A plugin on mapquery.core to add a legend to a layer. It will check if the layer
9 is within a valid extent and zoom range. And if not will return an error message.
13 $.extend( $.fn.mapQuery.defaults.layer.all, {
19 //possible error messages to display in the legend
20 LEGEND_ERRORS= ['E_ZOOMOUT', 'E_ZOOMIN', 'E_OUTSIDEBOX'];
21 $.extend(MQ.Layer.prototype, {
23 ###**layer**.`legend([options])`
25 ####**Description**: get/set the legend of a layer
27 **options** url:url the url to the legend image
29 >Returns: {url:url, msg:'E\_ZOOMOUT' | 'E\_ZOOMIN' | 'E\_OUTSIDEBOX' | ''}
32 The `.legend()` function allows us to attach a legend image to a layer. It will
33 also check if the layer is not visible due to wrong extent or zoom level.
34 It will return an error message which can be used to notify the user.
37 var legend = layer.legend(); //get the current legend
38 //set the legend url to legendimage.png
39 layer.legend({url:'legendimage.png'})
42 //get/set the legend object
43 legend: function(options) {
44 //get the legend object
45 var center = this.map.center();
46 if (arguments.length===0) {
47 this._checkZoom(center);
48 //if zoom = ok, check box
49 if(this.options.legend.msg==''){
50 this._checkBox(center);
52 return this.options.legend;
55 if (options.url!==undefined) {
56 this.options.legend.url = options.url;
57 return this.options.legend;
60 //Check if the layer has a maximum box set and if the current box
61 //is outside these settings, set the legend.msg accordingly
62 _checkBox: function(center){
63 var maxExtent = this.options.maxExtent;
64 if(maxExtent!==undefined) {
65 var mapBounds = new OpenLayers.Bounds(
66 center.box[0],center.box[1],center.box[2],center.box[3]);
67 var layerBounds = new OpenLayers.Bounds(
68 maxExtent[0],maxExtent[1],maxExtent[2],maxExtent[3]);
69 var inside = layerBounds.containsBounds(mapBounds, true);
70 this.options.legend.msg = inside?'':LEGEND_ERRORS[2];
73 //Check if the layer has a minimum or maximum zoom set and if the
74 //current zoom is outside these settings, set the legend.msg accordingly
75 _checkZoom: function(center){
76 var zoom = center.zoom;
77 var maxZoom = this.options.maxZoom;
78 var minZoom = this.options.minZoom;
79 this.options.legend.msg=(
80 maxZoom!==undefined&&maxZoom<zoom)? LEGEND_ERRORS[0]:'';
81 this.options.legend.msg=(
82 minZoom!==undefined&&minZoom>zoom)? LEGEND_ERRORS[1]:'';
87 })(jQuery, $.MapQuery);