aboutsummaryrefslogtreecommitdiff
path: root/gmap_arcgis.js
blob: 78a664eeec4c7a9e77b62d881a8b21762dc49e62 (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
// $Id$

function gmapArcgis(config) {
  return {
    config:         config,

    map:            config.map,

    deforestation:  [],

    // Add polygons from a given ArcGIS MapServer Layer
    showPolygon: function(element) {
      var defaultOpacity = (this.config['layers'][element].opacity != undefined) ? this.config['layers'][element].opacity : 0.55;
    
      if (typeof this.config['layers'][element].overlay != 'undefined') {
        this.config['layers'][element].overlay.setOpacity(defaultOpacity);
        this.unblockUI();
        return;
      }
    
      uri    = this.config['layers'][element]['uri'];
      layers = this.config['layers'][element]['layers'];
    
      this.config['layers'][element].overlay = new gmaps.ags.MapOverlay(uri, {
        exportOptions: {
            layerIds:    layers,
            layerOption: 'show',
        }
      });
    
      this.config['layers'][element].overlay.setMap(this.map);
      this.config['layers'][element].overlay.setOpacity(defaultOpacity);
    
      if (this.config['layers'][element].callback == undefined) {
        // Default callback: remove the blockUI once the layer is shown
        google.maps.event.addListener(this.config['layers'][element].overlay.getMapService(), 'update', function() {
          this.unblockUI();
        });
      }
      else {
        google.maps.event.addListener(params.overlay.getMapService(), 'update', function() {
          this.config['layers'][element].callback(this.map, this.config['layers'][element]) ; this.unblockUI();
        });
      }
    },
    
    // Add a KML to the map
    showKML: function(element) {
      this.config['kmls'][element].overlay = new google.maps.KmlLayer(this.config['kmls'][element].uri);
      this.config['kmls'][element].overlay.setMap(this.map);
    },

    // Add a KML layer into the map
    addKML: function(element) {
      if (typeof this.config['kmls'][element].overlay != 'undefined') {
        this.config['kmls'][element].overlay.setOpacity(this.config['kmls'][element].opacity);
        unblockUI();
        return this.config['kmls'][element].overlay;
      }
    
      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) {
      if (typeof this.config['layers'][element].overlay != 'undefined') {
        this.config['layers'][element].overlay.setOpacity(this.config['layers'][element].opacity);
        this.unblockUI();
        return this.config['layers'][element].overlay;
      }
    
      if (this.config['layers'][element].overlayTime != undefined) {
        this.showOverlay(this.config['layers'][element].overlayTime);
      }
    
      // TODO: support for clickable polygons
      this.showPolygon(element);
    },
    
    // Add makers to the map
    addMarkers: function(elements) {
      var bounds = this.map.getBounds();
    
      if (this.config['markers'][element].overlay != undefined) {
        this.toggleMarkers(this.config['markers'][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,
        // TODO
        //queryGeometry:  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;
        for (var i = 0, c = fs.length; i < c; i++) {
          fset = fs[i];
    
          this.config['markers'][element].overlay[i] = {};
          this.config['markers'][element].overlay[i].marker = new google.maps.Marker({
            title:    (this.config['markers'][element].title != undefined) ? fset.attributes[this.config['markers'][element].title] : fset.attributes.nome,
            icon:     this.config['markers'][element].icon,
            position: fset.geometry[0].getPosition(),
            map:      map,
          });
    
          // Use a closure so marker data remains available to the listeners
          this.config['markers'][element].overlay[i].addListener = function() {
            var marker     = this.config['markers'][element].overlay[i].marker;
            var infowindow = this.config['markers'][element].infowindow;
            var content    = this.config['markers'][element].content;
            var attributes = fset.attributes;
    
            google.maps.event.addListener(marker, 'click', function() {
              infowindow.setContent(content(attributes));
              infowindow.open(map, marker);
            });
          };
    
          // Execute
          this.config['markers'][element].overlay[i].addListener();
        }
    
        this.unblockUI();
      }
    },

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

    toggleKML: function(params) {
      // TODO
      return;
    },
    
    toggleLayers: function(element) {
      this.config['layers'][element].overlay.setOpacity(0);
    },
    
    // Toggle marker visibility
    toggleMarkers: function(element, raw) {
      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);
        }
      }
    },

    showOverlay: function(t) {
      if ($.browser.msie === false) {
        message = this.config.overlayMessage;
        if (t == false) {
          $.blockUI({
            message: message
          });
        } else {
          $.blockUI({
            message: message,
            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 deforestation
    deforestationUpdate: function(layer) {
      var defaultOpacity = 0.45;
    
      if (this.deforestation[layer] == undefined) {
        this.deforestation[layer] = this.addLayers({
          uri:         URL_ARCGIS_DESMATAMENTO,
          opacity:     defaultOpacity,
          layers:      [layer],
          overlayTime: 3000,
          callback:    updateDeforestationOpacity,
        });
      }
      else {
        updateDeforestationOpacity(map, { layers: [layer] });
      }
    
      function updateDeforestationOpacity(map, params) {
        for (var i in this.deforestation) {
          if (params.layers[0] == i) {
            this.deforestation[i].setOpacity(0.45);
          }
          else {
            this.deforestation[i].setOpacity(0);
          }
        }
    
        // The second and all subsequent layers shall be intialized without opacity.
        defaultOpacity = 0;
      }
    
    },

    setMap: function(map) {
      this.map = map;
    },

  }
};