source: Dev/trunk/src/client/dojox/geo/openlayers/Map.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 15.9 KB
Line 
1define([
2        "dojo/_base/kernel",
3        "dojo/_base/declare",
4        "dojo/_base/lang",
5        "dojo/_base/array",
6        "dojo/_base/json",
7        "dojo/dom",
8        "dojo/dom-style",
9        "./_base",
10        "./TouchInteractionSupport",
11        "./Layer",
12        "./Patch"
13], function(kernel, declare, lang, array, json, dom, style, openlayers, TouchInteractionSupport, Layer, Patch){
14
15        kernel.experimental("dojox.geo.openlayers.Map");
16
17
18        Patch.patchGFX();
19
20        /*=====
21        dojox.geo.openlayers.__MapArgs = {
22                // summary:
23                //              The keyword arguments that can be passed in a Map constructor.
24                // baseLayerType: String
25                //               type of the base layer. Can be any of
26                //
27                //              - `dojox.geo.openlayers.BaseLayerType.OSM`: Open Street Map base layer
28                //              - `dojox.geo.openlayers.BaseLayerType.WMS`: Web Map Service layer
29                //              - `dojox.geo.openlayers.BaseLayerType.GOOGLE`: Google layer
30                //              - `dojox.geo.openlayers.BaseLayerType.VIRTUAL_EARTH`: Virtual Earth layer
31                //              - `dojox.geo.openlayers.BaseLayerType.BING`: Bing layer
32                //              - `dojox.geo.openlayers.BaseLayerType.YAHOO`: Yahoo layer
33                //              - `dojox.geo.openlayers.BaseLayerType.ARCGIS`: ESRI ArgGIS layer
34                // baseLayerName: String
35                //              The name of the base layer.
36                // baseLayerUrl: String
37                //              Some layer may need an url such as Web Map Server.
38                // baseLayerOptions: String
39                //              Additional specific options passed to OpensLayers layer, such as The list of layer to display, for Web Map Server layer.
40        };
41        =====*/
42
43        return declare("dojox.geo.openlayers.Map", null, {
44                // summary:
45                //              A map viewer based on the OpenLayers library.
46                //
47                // description:
48                //              The `dojox.geo.openlayers.Map` object allows to view maps from various map providers.
49                //              It encapsulates  an `OpenLayers.Map` object on which most operations are delegated.
50                //              GFX layers can be added to display GFX georeferenced shapes as well as Dojo widgets.
51                //              Parameters can be passed as argument at construction time to define the base layer
52                //              type and the base layer parameters such as url or options depending on the type
53                //              specified. These parameters can be any of:
54                //
55                //              _baseLayerType_: type of the base layer. Can be any of:
56                //             
57                //              - `dojox.geo.openlayers.BaseLayerType.OSM`: Open Street Map base layer
58                //              - `dojox.geo.openlayers.BaseLayerType.WMS`: Web Map Service layer
59                //              - `dojox.geo.openlayers.BaseLayerType.GOOGLE`: Google layer
60                //              - `dojox.geo.openlayers.BaseLayerType.VIRTUAL_EARTH`: Virtual Earth layer
61                //              - `dojox.geo.openlayers.BaseLayerType.BING`: Bing layer
62                //              - `dojox.geo.openlayers.BaseLayerType.YAHOO`: Yahoo layer
63                //              - `dojox.geo.openlayers.BaseLayerType.ARCGIS`: ESRI ArgGIS layer
64                //             
65                //              Note that access to commercial server such as Google, Virtual Earth or Yahoo may need specific licencing.
66                //             
67                //              The parameters value also include:
68                //             
69                //              - `baseLayerName`: The name of the base layer.
70                //              - `baseLayerUrl`: Some layer may need an url such as Web Map Server
71                //              - `baseLayerOptions`: Additional specific options passed to OpensLayers layer,
72                //                such as The list of layer to display, for Web Map Server layer.
73                //
74                // example:
75                //      |       var map = new dojox.geo.openlayers.widget.Map(div, {
76                //      |               baseLayerType: dojox.geo.openlayers.BaseLayerType.OSM,
77                //      |               baseLayerName: 'Open Street Map Layer'
78                //      |       });
79
80                // olMap: OpenLayers.Map
81                //              The underlying OpenLayers.Map object.
82                //              Should be accessed on read mode only.
83                olMap: null,
84
85                _tp: null,
86
87                constructor: function(div, options){
88                        // summary:
89                        //              Constructs a new Map object
90                        if(!options){
91                                options = {};
92                        }
93
94                        div = dom.byId(div);
95
96                        this._tp = {
97                                x: 0,
98                                y: 0
99                        };
100
101                        var opts = options.openLayersMapOptions;
102
103                        if(!opts){
104                                opts = {
105                                        controls: [new OpenLayers.Control.ScaleLine({
106                                                maxWidth: 200
107                                        }), new OpenLayers.Control.Navigation()]
108                                };
109                        }
110                        if(options.accessible){
111                                var kbd = new OpenLayers.Control.KeyboardDefaults();
112                                if(!opts.controls){
113                                        opts.controls = [];
114                                }
115                                opts.controls.push(kbd);
116                        }
117                        var baseLayerType = options.baseLayerType;
118                        if(!baseLayerType){
119                                baseLayerType = openlayers.BaseLayerType.OSM;
120                        }
121                        var map = new OpenLayers.Map(div, opts);
122                        this.olMap = map;
123
124                        this._layerDictionary = {
125                                olLayers: [],
126                                layers: []
127                        };
128
129                        if(options.touchHandler){
130                                this._touchControl = new TouchInteractionSupport(map);
131                        }
132
133                        var base = this._createBaseLayer(options);
134                        this.addLayer(base);
135
136                        this.initialFit(options);
137                },
138
139                initialFit: function(params){
140                        // summary:
141                        //              Performs an initial fit to contents.
142                        // tags:
143                        //              protected
144                        var o = params.initialLocation;
145                        if(!o){
146                                o = [-160, 70, 160, -70];
147                        }
148                        this.fitTo(o);
149                },
150
151                setBaseLayerType: function(type){
152                        // summary:
153                        //              Set the base layer type, replacing the existing base layer
154                        // type: dojox/geo/openlayers.BaseLayerType
155                        //              base layer type
156                        // returns:
157                        //              The newly created layer.
158                        if(type == this.baseLayerType){
159                                return null; // Layer
160                        }
161
162                        var o = null;
163                        if(typeof type == "string"){
164                                o = {
165                                        baseLayerName: type,
166                                        baseLayerType: type
167                                };
168                                this.baseLayerType = type;
169                        }else if(typeof type == "object"){
170                                o = type;
171                                this.baseLayerType = o.baseLayerType;
172                        }
173                        var bl = null;
174                        if(o != null){
175                                bl = this._createBaseLayer(o);
176                                if(bl != null){
177                                        var olm = this.olMap;
178                                        var ob = olm.getZoom();
179                                        var oc = olm.getCenter();
180                                        var recenter = !!oc && !!olm.baseLayer && !!olm.baseLayer.map;
181
182                                        if(recenter){
183                                                var proj = olm.getProjectionObject();
184                                                if(proj != null){
185                                                        oc = oc.transform(proj, openlayers.EPSG4326);
186                                                }
187                                        }
188                                        var old = olm.baseLayer;
189                                        if(old != null){
190                                                var l = this._getLayer(old);
191                                                this.removeLayer(l);
192                                        }
193                                        if(bl != null){
194                                                this.addLayer(bl);
195                                        }
196                                        if(recenter){
197                                                proj = olm.getProjectionObject();
198                                                if(proj != null){
199                                                        oc = oc.transform(openlayers.EPSG4326, proj);
200                                                }
201                                                olm.setCenter(oc, ob);
202                                        }
203                                }
204                        }
205                        return bl;
206                },
207
208                getBaseLayerType: function(){
209                        // summary:
210                        //              Returns the base layer type.
211                        // returns:
212                        //              The current base layer type.
213                        return this.baseLayerType; // openlayers.BaseLayerType
214                },
215
216                getScale: function(geodesic){
217                        // summary:
218                        //              Returns the current scale
219                        // geodesic: Boolean
220                        //              Tell if geodesic calculation should be performed. If set to
221                        //              true, the scale will be calculated based on the horizontal size of the
222                        //              pixel in the center of the map viewport.
223                        var scale = null;
224                        var om = this.olMap;
225                        if(geodesic){
226                                var units = om.getUnits();
227                                if(!units){
228                                        return null;    // Number
229                                }
230                                var inches = OpenLayers.INCHES_PER_UNIT;
231                                scale = (om.getGeodesicPixelSize().w || 0.000001) * inches["km"] * OpenLayers.DOTS_PER_INCH;
232                        }else{
233                                scale = om.getScale();
234                        }
235                        return scale;   // Number
236                },
237
238                getOLMap: function(){
239                        // summary:
240                        //              gets the underlying OpenLayers map object.
241                        // returns:
242                        //              The underlying OpenLayers map object.
243                        return this.olMap;      // OpenLayers.Map
244                },
245
246                _createBaseLayer: function(params){
247                        // summary:
248                        //              Creates the base layer.
249                        // tags:
250                        //              private
251                        var base = null;
252                        var type = params.baseLayerType;
253                        var url = params.baseLayerUrl;
254                        var name = params.baseLayerName;
255                        var options = params.baseLayerOptions;
256
257                        if(!name){
258                                name = type;
259                        }
260                        if(!options){
261                                options = {};
262                        }
263                        switch(type){
264                                case openlayers.BaseLayerType.OSM:
265                                        options.transitionEffect = "resize";
266                                        //                              base = new OpenLayers.Layer.OSM(name, url, options);
267                                        base = new Layer(name, {
268                                                olLayer: new OpenLayers.Layer.OSM(name, url, options)
269                                        });
270                                break;
271                                case openlayers.BaseLayerType.WMS:
272                                        if(!url){
273                                                url = "http://labs.metacarta.com/wms/vmap0";
274                                                if(!options.layers){
275                                                        options.layers = "basic";
276                                                }
277                                        }
278                                        base = new Layer(name, {
279                                                olLayer: new OpenLayers.Layer.WMS(name, url, options, {
280                                                        transitionEffect: "resize"
281                                                })
282                                        });
283                                break;
284                                case openlayers.BaseLayerType.GOOGLE:
285                                        base = new Layer(name, {
286                                                olLayer: new OpenLayers.Layer.Google(name, options)
287                                        });
288                                break;
289                                case openlayers.BaseLayerType.VIRTUAL_EARTH:
290                                        base = new Layer(name, {
291                                                olLayer: new OpenLayers.Layer.VirtualEarth(name, options)
292                                        });
293                                break;
294                                case openlayers.BaseLayerType.YAHOO:
295                                        //                              base = new OpenLayers.Layer.Yahoo(name);
296                                        base = new Layer(name, {
297                                                olLayer: new OpenLayers.Layer.Yahoo(name, options)
298                                        });
299                                break;
300                                case openlayers.BaseLayerType.ARCGIS:
301                                        if(!url){
302                                                url = "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer/export";
303                                        }
304                                        base = new Layer(name, {
305                                                olLayer: new OpenLayers.Layer.ArcGIS93Rest(name, url, options, {})
306                                        });
307
308                                break;
309                        }
310
311                        if(base == null){
312                                if(type instanceof OpenLayers.Layer){
313                                        base = type;
314                                }else{
315                                        options.transitionEffect = "resize";
316                                        base = new Layer(name, {
317                                                olLayer: new OpenLayers.Layer.OSM(name, url, options)
318                                        });
319                                        this.baseLayerType = openlayers.BaseLayerType.OSM;
320                                }
321                        }
322
323                        return base;
324                },
325
326                removeLayer: function(layer){
327                        // summary:
328                        //              Remove the specified layer from the map.
329                        // layer: Layer
330                        //              The layer to remove from the map.
331                        var om = this.olMap;
332                        var i = array.indexOf(this._layerDictionary.layers, layer);
333                        if(i > 0){
334                                this._layerDictionary.layers.splice(i, 1);
335                        }
336                        var oll = layer.olLayer;
337                        var j = array.indexOf(this._layerDictionary.olLayers, oll);
338                        if(j > 0){
339                                this._layerDictionary.olLayers.splice(i, j);
340                        }
341                        om.removeLayer(oll, false);
342                },
343
344                layerIndex: function(layer, index){
345                        // summary:
346                        //              Set or retrieve the layer index.
347                        // description:
348                        //              Set or get the layer index, that is the z-order of the layer.
349                        //              if the index parameter is provided, the layer index is set to
350                        //              this value. If the index parameter is not provided, the index of
351                        //              the layer is returned.
352                        // layer: Layer
353                        //              the layer to retrieve the index.
354                        // index: int?
355                        //              index of the layer
356                        // returns:
357                        //              the index of the layer.
358                        var olm = this.olMap;
359                        if(!index){
360                                return olm.getLayerIndex(layer.olLayer);
361                        }
362                        //olm.raiseLayer(layer.olLayer, index);
363                        olm.setLayerIndex(layer.olLayer, index);
364
365                        this._layerDictionary.layers.sort(function(l1, l2){
366                                return olm.getLayerIndex(l1.olLayer) - olm.getLayerIndex(l2.olLayer);
367                        });
368                        this._layerDictionary.olLayers.sort(function(l1, l2){
369                                return olm.getLayerIndex(l1) - olm.getLayerIndex(l2);
370                        });
371
372                        return index; // Number
373                },
374
375                addLayer: function(layer){
376                        // summary:
377                        //              Add the specified layer to the map.
378                        // layer: Layer
379                        //              The layer to add to the map.
380                        layer.dojoMap = this;
381                        var om = this.olMap;
382                        var ol = layer.olLayer;
383                        this._layerDictionary.olLayers.push(ol);
384                        this._layerDictionary.layers.push(layer);
385                        om.addLayer(ol);
386                        layer.added();
387                },
388
389                _getLayer: function(/*OpenLayer.Layer */ol){
390                        // summary:
391                        //              Retrieve the dojox.geo.openlayer.Layer from the OpenLayer.Layer
392                        // tags:
393                        //              private
394                        var i = array.indexOf(this._layerDictionary.olLayers, ol);
395                        if(i != -1){
396                                return this._layerDictionary.layers[i];
397                        }
398                        return null;
399                },
400
401                getLayer: function(property, value){
402                        // summary:
403                        //              Returns the layer whose property matches the value.
404                        // property: String
405                        //              The property to check
406                        // value: Object
407                        //              The value to match
408                        // returns:
409                        //              The layer(s) matching the property's value. Since multiple layers
410                        //              match the property's value the return value is an array.
411                        // example:
412                        //              var layers = map.getLayer("name", "Layer Name");
413                        var om = this.olMap;
414                        var ols = om.getBy("layers", property, value);
415                        var ret = new Array(); //[];
416                        array.forEach(ols, function(ol){
417                                ret.push(this._getLayer(ol));
418                        }, this);
419                        return ret; // Layer[]
420                },
421
422                getLayerCount: function(){
423                        // summary:
424                        //              Returns the count of layers of this map.
425                        // returns:
426                        //              The number of layers of this map.
427                        var om = this.olMap;
428                        if(om.layers == null){
429                                return 0;
430                        }
431                        return om.layers.length; // Number
432                },
433
434                fitTo: function(o){
435                        // summary:
436                        //              Fits the map on a point,or an area
437                        // description:
438                        //              Fits the map on the point or extent specified as parameter.
439                        // o: Object
440                        //              Object with key values fit parameters or a JSON string.
441                        // example:
442                        //              Examples of arguments passed to the fitTo function:
443                        //      |       null
444                        //              The map is fit on full extent
445                        //
446                        //      |       {
447                        //      |               bounds: [ulx, uly, lrx, lry]
448                        //      |       }
449                        //              The map is fit on the specified bounds expressed as decimal degrees latitude and longitude.
450                        //              The bounds are defined with their upper left and lower right corners coordinates.
451                        //
452                        //      |       {
453                        //      |               position: [longitude, latitude],
454                        //      |               extent: degrees
455                        //      |       }
456                        //              The map is fit on the specified position showing the extent `<extent>` around
457                        //              the specified center position.
458
459                        var map = this.olMap;
460                        var from = openlayers.EPSG4326;
461
462                        if(o == null){
463                                var c = this.transformXY(0, 0, from);
464                                map.setCenter(new OpenLayers.LonLat(c.x, c.y));
465                                return;
466                        }
467                        var b = null;
468                        if(typeof o == "string"){
469                                var j = json.fromJson(o);
470                        }else{
471                                j = o;
472                        }
473                        var ul;
474                        var lr;
475                        if(j.hasOwnProperty("bounds")){
476                                var a = j.bounds;
477                                b = new OpenLayers.Bounds();
478                                ul = this.transformXY(a[0], a[1], from);
479                                b.left = ul.x;
480                                b.top = ul.y;
481                                lr = this.transformXY(a[2], a[3], from);
482                                b.right = lr.x;
483                                b.bottom = lr.y;
484                        }
485                        if(b == null){
486                                if(j.hasOwnProperty("position")){
487                                        var p = j.position;
488                                        var e = j.hasOwnProperty("extent") ? j.extent : 1;
489                                        if(typeof e == "string"){
490                                                e = parseFloat(e);
491                                        }
492                                        b = new OpenLayers.Bounds();
493                                        ul = this.transformXY(p[0] - e, p[1] + e, from);
494                                        b.left = ul.x;
495                                        b.top = ul.y;
496                                        lr = this.transformXY(p[0] + e, p[1] - e, from);
497                                        b.right = lr.x;
498                                        b.bottom = lr.y;
499                                }
500                        }
501                        if(b == null){
502                                if(o.length == 4){
503                                        b = new OpenLayers.Bounds();
504                                        // TODO Choose the correct method
505                                        if(false){
506                                                b.left = o[0];
507                                                b.top = o[1];
508
509                                                b.right = o[2];
510                                                b.bottom = o[3];
511                                        }else{
512                                                ul = this.transformXY(o[0], o[1], from);
513                                                b.left = ul.x;
514                                                b.top = ul.y;
515                                                lr = this.transformXY(o[2], o[3], from);
516                                                b.right = lr.x;
517                                                b.bottom = lr.y;
518                                        }
519                                }
520                        }
521                        if(b != null){
522                                map.zoomToExtent(b, true);
523                        }
524                },
525
526                transform: function(p, from, to){
527                        // summary:
528                        //              Transforms the point passed as argument, expressed in the <em>from</em>
529                        //              coordinate system to the map coordinate system.
530                        // description:
531                        //              Transforms the point passed as argument without modifying it. The point is supposed to be expressed
532                        //              in the <em>from</em> coordinate system and is transformed to the map coordinate system.
533                        // p: Object {x, y}
534                        //              The point to transform
535                        // from: OpenLayers.Projection
536                        //              The projection in which the point is expressed.
537                        return this.transformXY(p.x, p.y, from, to);
538                },
539
540                transformXY: function(x, y, from, to){
541                        // summary:
542                        //              Transforms the coordinates passed as argument, expressed in the <em>from</em>
543                        //              coordinate system to the map coordinate system.
544                        // description:
545                        //              Transforms the coordinates passed as argument. The coordinate are supposed to be expressed
546                        //              in the <em>from</em> coordinate system and are transformed to the map coordinate system.
547                        // x: Number
548                        //              The longitude coordinate to transform.
549                        // y: Number
550                        //              The latitude coordinate to transform.
551                        // from: OpenLayers.Projection?
552                        //              The projection in which the point is expressed, or EPSG4326 is not specified.
553                        // to: OpenLayers.Projection?
554                        //              The projection in which the point is converted to. In not specifed, the map projection is used.
555                        // returns:
556                        //              The transformed coordinate as an {x,y} Object.
557
558                        var tp = this._tp;
559                        tp.x = x;
560                        tp.y = y;
561                        if(!from){
562                                from = openlayers.EPSG4326;
563                        }
564                        if(!to){
565                                to = this.olMap.getProjectionObject();
566                        }
567                        tp = OpenLayers.Projection.transform(tp, from, to);
568                        return tp; // Object
569                }
570
571        });
572
573});
Note: See TracBrowser for help on using the repository browser.