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

// Load Google Maps API
google.load("maps", "2.x");

// Global variables
var map, mapExtension, gOverlays;

// Setup custom GMap Overlays (layers and labels)
function gmap_arcgis() {
  /**
   * Use a timer to make sure everything is loaded upon execution time.
   * See http://drupal.org/node/223613#comment-736303
   */
  setTimeout(function() {
    // Variables
    var polygons = new Array();
    var labels   = new Array();
    var fields   = new Array();
    var id       = 'auto1map';
    var content, label;
   
    // Get data from Drupal
    if (Drupal.settings.gmap_arcgis !== undefined) {
      // Get array of labels
      if (Drupal.settings.gmap_arcgis.labels !== undefined) {
        labels = Drupal.settings.gmap_arcgis.labels;
      }
      // Get Map Id
      if (Drupal.settings.gmap_arcgis.id !== undefined) {
        id = Drupal.settings.gmap_arcgis.id;
      }
    }
  
    // Get an existing GMap instance using Drupal GMap Module API
    // and add an ArcGIS extension to it
    map          = Drupal.gmap.getMap(id).map;
    mapExtension = new esri.arcgis.gmaps.MapExtension(map);
  
    for (var i = 0; i < labels.length; i++) {
      label = labels[i];
      // Set default fallback values for labels
      fields  = ["id", "name"];
      content = 'Placeholder';
      icon    = new GIcon(G_DEFAULT_ICON);

      if (label[1] !== undefined) {
        // Set custom fields
        fields = [ label[1][0], label[1][1] ];
      }
  
      if (label[2] !== undefined) {
        // Set custom content
        content = label[2];
      }
  
      if (label[3] !== undefined) {
        // Set label icons
        var info              = label[3];
        icon.shadow           = info[0];
        icon.image            = info[1];
        icon.infoSize         = new  GSize(info[2][0], info[2][1]);
        icon.shadowSize       = new  GSize(info[3][0], info[3][1]);
        icon.infoAnchor       = new GPoint(info[4][0], info[4][1]);
        icon.infoWindowAnchor = new GPoint(info[5][0], info[5][1]);
      }

      GEvent.addListener(map, "moveend", function() { showLabels(label[0], fields, content, icon); });
      showLabels(label[0], fields, content, icon);
    }
  
    gmapArcgisShowPolygons(Drupal.settings.gmap_arcgis.polygons);
    
  }, 5);
  //setTimeout(unblockUI, 10000);
}

// Add labels from a given ArcGIS MapServer Layer
function showLabels(label, fields, content, icon) {
  var bounds    = map.getBounds();
  var queryTask = new esri.arcgis.gmaps.QueryTask(label);
  var query     = new esri.arcgis.gmaps.Query();

  // clear gOverlays overlays and event listeners
  //mapExtension.removeFromMap(gOverlays);

  // set query parameters
  query.queryGeometry  = bounds;
  query.returnGeometry = true;
  query.outFields      = fields;

  // set the callback
  var callback = showLabelsCallback(fields, content, icon);

  // execute query task
  queryTask.execute(query, false, callback);
}

// Callback for showLabels
function showLabelsCallback(fields, content, icon) {
  return function(fset) {
    // Label title
    var title = '{' + fields[1] + '}';
    
    // JS literal class esri.arcgis.gmaps.MarkerOptions
    var myMarkerOptions = { title:title, icon:icon };

    // JS literal class esri.arcgis.gmaps.OverlayOptions
    var overlayOptions = { markerOptions:myMarkerOptions };

    // JS literal class esri.arcgis.gmaps.InfoWindowOptions without tabs
    var infoWindowOptions = { content: content };

    gOverlays = mapExtension.addToMap(fset, overlayOptions, infoWindowOptions);
  }
}

// Show all defined polygons
function gmapArcgisShowPolygons(polygons) {
  for (j = 0; j < polygons.length; j++) {
    showPolygon(polygons[j]);
  }
}

// Hide all defined polygons
function gmapArcgisHidePolygons(polygons) {
  for (j = 0; j < polygons.length; j++) {
    polygons[j].overlay.setOpacity(0);
    unblockUI();
  }
}

// Add polygons from a given ArcGIS MapServer Layer
function showPolygon(polygon) {
  var defaultOpacity = 0.55;

  if (typeof polygon.overlay != 'undefined') {
    polygon.overlay.setOpacity(defaultOpacity);
    unblockUI();
    return;
  }

  uri    = polygon['base'] + polygon['service'];
  layers = polygon['layers'];

  polygon.overlay = new esri.arcgis.gmaps.DynamicMapServiceLayer(uri, null, defaultOpacity);

  if (layers != undefined || layers == []) {
    polygon.overlay.setVisibleLayers(layers);
  }

  GEvent.addListener(polygon.overlay, "load", showPolygonCallback);
  GEvent.addListener(polygon.overlay, "update", function() { unblockUI(); });
}

// Callback for showPolygon
function showPolygonCallback(mapservicelayer, error) {
  map.addOverlay(mapservicelayer);
  setTimeout(unblockUI, 6000);
}

// Add custom marker
function createMarker(point, name, html, icone) {
  var icon              = new GIcon();
  icon.image            = icone;
  icon.shadow           = "http://maps.google.com/mapfiles/ms/micons/msmarker.shadow.png";
  icon.iconSize         = new GSize(20, 20);
  icon.shadowSize       = new GSize(22, 20);
  icon.iconAnchor       = new GPoint(6, 20);
  icon.infoWindowAnchor = new GPoint(5, 1);

  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
  return marker;
}

function unblockUI() {
  $.unblockUI();
}

// Add map overlays
$(document).ready(function() {
  google.setOnLoadCallback(gmap_arcgis);
  $('#edit-lista-uc').change(function() {
      oelem = $("option:selected", this).val();
      marcador = Drupal.gmap.getMap('auto1map').vars.markers[oelem];
      GEvent.trigger(marcador.marker,"click");
      $('#header').hide();
      $('#nav').hide();
  });
});