aboutsummaryrefslogtreecommitdiff
path: root/gmap_arcgis.js
blob: 9a9b4db52f42683bbe089a3be3e0cdb5d3b41f7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// $Id$

function gmapArcgis(config) {
  return {
    // Storable properties
    config: config,
    map:    config.map,

    // UI unblocker wrapper
    unblockUI: function() {
      jQuery.unblockUI();
    },

    // UI blocker wrapper
    blockUI: function() {
      jQuery.blockUI();
    },

    // Set the current map
    setMap: function(map) {
      this.map = map;
    },

    // Get the default opacity of an layer element
    defaultOpacity: function(element, store) {
      store = (store != undefined) ? store : 'layers';

      if (this.config[store][element] != undefined && this.config[store][element].opacity != undefined) {
        return this.config[store][element].opacity;
      }
      else {
        return 0.55;
      }
    },

    // Add polygons from a given ArcGIS MapServer Layer
    showPolygon: function(params, store) {
      params.service = new gmaps.ags.MapService(params['uri']);
      params.overlay = new gmaps.ags.MapOverlay(params.service, {
        exportOptions: {
            layerIds:    params['layers'],
            layerOption: 'show',
        }
      });
    
      params.overlay.setMap(this.map);
      params.overlay.setOpacity(params.opacity);
    
      this.identifyPolygon(params);

      if (params.callback == undefined) {
        // Default callback: remove the blockUI once the layer is shown
        google.maps.event.addListener(params.overlay.getMapService(), 'update', function() {
          this.unblockUI();
        });
      }
      else {
        google.maps.event.addListener(params.overlay.getMapService(), 'update', function() {
          params.callback(this.map, params) ; this.unblockUI();
        });
      }

      return params.overlay;
    },

    // Add an identify callback to a layer
    identifyPolygon: function(params) {
      // See https://google-maps-utility-library-v3.googlecode.com/svn/trunk/arcgislink/examples/identify.js
      if (typeof params.identify != 'undefined') {
        var self = this;

        // Configure event listener
        params.identifyListener = google.maps.event.addListener(this.map, 'click', function(point) {
          params.identify.geometry = point.latLng;
          params.identify.bounds   = self.map.getBounds();
          params.identify.width    = self.map.getDiv().offsetWidth;
          params.identify.height   = self.map.getDiv().offsetHeight;

          // Configure infoWindow
          if (params.infowindow != undefined) {
            params.infowindow.close();
            params.infowindow.setContent('');
            params.infowindow.setPosition(point.latLng);
          }
          else {
            params.infowindow = new google.maps.InfoWindow({
              content:  '',
              position: point.latLng,
            });
          }

          // Identify layers
          params.service.identify(params.identify, function(identify, err) {
            if (!err) {
              var layers  = { };
              var result  = null;
              var layerId = null;

              // Sort results by layer
              for (var i in identify.results) {
                result  = identify.results[i];
                layerId = result.layerId;

                if (layers[layerId] == undefined) {
                  layers[layerId] = [ result ];
                }
                else {
                  layers[layerId].push(result);
                }
              }

              // Callback only if layers is not empty
              if (layers.toSource() !== "({})") {
                params.identify.callback(layers, params.infowindow, err);
              }
            }
          });
        });
      }
    },

    // Add a KML to the map
    showKML: function(element) {
      if (typeof this.config['kmls'][element].overlay == 'undefined') {
        this.config['kmls'][element].overlay = new google.maps.KmlLayer(this.config['kmls'][element].uri);
      }

      this.config['kmls'][element].overlay.setMap(this.map);
      this.config['kmls'][element].isVisible = true;
    },

    // Add a KML layer into the map
    addKML: function(element) {
      if (typeof this.config['kmls'][element].overlay != 'undefined') {
        this.showKML(element);
        this.unblockUI();
        return;
      }
    
      if (this.config['kmls'][element].overlayTime != undefined) {
        this.showOverlay(this.config['kmls'][element].overlayTime);
      }
    
      this.showKML(element);
    },
    
    // Add a layer to the map
    addLayers: function(element) {
      this.config['layers'][element].opacity = this.defaultOpacity(element);

      if (typeof this.config['layers'][element].overlay != 'undefined') {
        this.config['layers'][element].overlay.setOpacity(this.config['layers'][element].opacity);
        this.identifyPolygon(this.config['layers'][element]);
        this.unblockUI();
        return;
      }
    
      if (this.config['layers'][element].overlayTime != undefined) {
        this.showOverlay(this.config['layers'][element].overlayTime);
      }

      this.config['layers'][element].overlay = this.showPolygon(this.config['layers'][element]);
    },

    // Add a slider layer in the map
    addSlider: function(layer, slider) {
      this.config['sliders'][slider].opacity  = this.defaultOpacity(layer, 'sliders');
      this.config['sliders'][slider].callback = this.sliderUpdateOpacity;
      this.config['sliders'][slider].layers   = [layer];

      if (typeof this.config['sliders'][slider].overlays[layer] != 'undefined') {
        this.config['sliders'][slider].overlays[layer].setOpacity(this.config['sliders'][slider].opacity);
        this.unblockUI();
      }

      if (this.config['sliders'][slider].overlayTime != undefined) {
        this.showOverlay(this.config['sliders'][slider].overlayTime);
      }

      this.config['sliders'][slider].overlays[layer] = this.showPolygon(this.config['sliders'][slider]);
    },
    
    // Add makers to the map
    addMarkers: function(element) {
      var bounds = this.map.getBounds();
      var self   = this;
    
      if (this.config['markers'][element].overlay != undefined) {
        this.toggleMarkers(element);
        return;
      } else {
        this.config['markers'][element].overlay = [];
      }
    
      if (this.config['markers'][element].overlayTime != undefined) {
        this.showOverlay(this.config['markers'][element].overlayTime);
      }
    
      // Query parameters
      var query = {
        returnGeometry: true,
        geometry:       bounds,
        where:          (this.config['markers'][element].where != undefined) ? this.config['markers'][element].where : '1=1',
        outFields:      this.config['markers'][element].fields,
      };
    
      var layer = new gmaps.ags.Layer(this.config['markers'][element].uri);
    
      // Execute query
      layer.query(query, processResultSet); 
    
      // Configure infoWindow
      this.config['markers'][element].infowindow = new google.maps.InfoWindow({
        content: '',
      });
    
      // Process results
      function processResultSet(fset) {
        var fs = fset.features;
        var title;
        for (var i = 0, c = fs.length; i < c; i++) {
          fset = fs[i];

          if (self.config['markers'][element].title != undefined) {
            title = fset.attributes[self.config['markers'][element].title];
          }
          else {
            title = fset.attributes.nome;
          }
    
          self.config['markers'][element].overlay[i] = {};
          self.config['markers'][element].overlay[i].marker = new google.maps.Marker({
            title:    title,
            icon:     self.config['markers'][element].icon,
            position: fset.geometry[0].getPosition(),
            map:      map,
          });
    
          // Use a closure so marker data remains available to the listeners
          self.config['markers'][element].overlay[i].addListener = function() {
            var marker     = self.config['markers'][element].overlay[i].marker;
            var infowindow = self.config['markers'][element].infowindow;
            var content    = self.config['markers'][element].content;
            var attributes = fset.attributes;
    
            google.maps.event.addListener(marker, 'click', function() {
              infowindow.setContent(content(attributes));
              infowindow.open(map, marker);
            });
          };
    
          // Execute
          self.config['markers'][element].overlay[i].addListener();
        }
    
        self.unblockUI();
      }
    },

    // Toggle KML visibility
    toggleKML: function(element) {
      if (this.config['kmls'][element].overlay == undefined) {
        return;
      } else if (this.config['kmls'][element].isVisible == true) {
        this.config['kmls'][element].isVisible = false;
        this.config['kmls'][element].overlay.setMap();
      } else {
        this.config['kmls'][element].isVisible = true;
        this.config['kmls'][element].overlay.setMap(this.map);
      }
    },
    
    // Toggle layer visibility
    toggleLayers: function(element) {
      this.config['layers'][element].overlay.setOpacity(0);

      if (this.config['layers'][element].identifyListener != undefined) {
        google.maps.event.removeListener(this.config['layers'][element].identifyListener);
      }
    },
    
    // Toggle marker visibility
    toggleMarkers: function(element, raw) {
      var markers = (raw == true) ? element : this.config['markers'][element].overlay;
    
      for (i=0; i < markers.length; i++) {
        if (markers[i].marker.getVisible()) {
          markers[i].marker.setVisible(false);
        } else {
          markers[i].marker.setVisible(true);
        }
      }
    },

    // Block the UI and show an overlay
    showOverlay: function(t) {
      if ($.browser.msie === false) {
        message = this.config.overlayMessage;

        if (this.config.overlayCss != undefined) {
          css = this.config.overlayCss;
        }
        else {
          css = { };
        }

        if (t == false) {
          $.blockUI({
            message: message,
            css:     css,
          });
        } else {
          $.blockUI({
            message: message,
            css:     css,
            timeout: t,
          });
        }
      }
    },

    // Change a element in the map
    changeElement: function(type, element, callback) {
      if (this.config[type][element] == undefined || this.config[type][element]['showOn'] == undefined) {
        return;
      }
    
      if (jQuery.inArray(this.config.mapName, this.config[type][element]['showOn']) != -1) {
        this[callback](element);
      }
    },
    
    // Display slider
    sliderUpdate: function(layer, slider) {
      if (this.config['sliders'][slider].overlays == undefined) {
        this.config['sliders'][slider].overlays = [ ];
      }

      if (this.config['sliders'][slider].overlays[layer] == undefined) {
        this.addSlider(layer, slider);
      }

      this.sliderUpdateOpacity(layer, slider);
    },

    // Fix slider opacity
    sliderUpdateOpacity: function(layer, slider) {
      for (var i in this.config['sliders'][slider].overlays) {
        if (i == layer) {
          this.config['sliders'][slider].overlays[i].setOpacity(this.defaultOpacity(slider, 'sliders'));
        }
        else {
          this.config['sliders'][slider].overlays[i].setOpacity(0);
        }
      }
    
      // The second and all subsequent layers shall be intialized without opacity.
      this.sliderOpacity = 0;
    },

    // jQuery slider wrapper
    slider: function(element, slider) {
      var self = this;

      // Max is the number of layers in the service
      $(element).slider({
          value: 1,
          min:  -1,
          max:  11,
          step:  1,
          animate: true,
          change:  function(event, ui) {
            self.sliderUpdate.apply(self, [ ui.value, slider ]);
          },
      });
    },

  }
};