2 * Description: display a query result in a Google map
3 * Copyright (c) 2012-2013 UPMC Sorbonne Universite - INRIA
8 * - infowindow is not properly reopened when the maps has not the focus
13 var GoogleMaps = Plugin.extend({
15 init: function(options, element)
17 this._super(options, element);
19 /* Member variables */
21 this.received_all = false;
22 this.received_set = false;
23 this.in_set_buffer = Array();
25 // key -> { marker, checked }
29 this.el().on('show', this, this.on_show);
31 // $(window).unbind('Hazelnut');
33 var query = manifold.query_store.find_analyzed_query(this.options.query_uuid);
34 this.method = query.object;
36 var keys = manifold.metadata.get_key(this.method);
37 this.key = (keys && keys.length == 1) ? keys[0] : null;
39 /* Setup query and record handlers */
40 this.listen_query(options.query_uuid);
41 this.listen_query(options.query_all_uuid, 'all');
43 /* GUI setup and event binding */
44 this.initialize_map();
52 google.maps.event.trigger(self.map, 'resize');
57 /* GUI MANIPULATION */
61 initialize_map: function()
63 this.markerCluster = null;
64 this.coords = new Array();
66 var myLatlng = new google.maps.LatLng(this.options.latitude, this.options.longitude);
68 zoom: this.options.zoom,
70 mapTypeId: google.maps.MapTypeId.ROADMAP
73 var id = this.options.plugin_uuid + manifold.separator + 'map';
74 this.map = new google.maps.Map(document.getElementById(id), myOptions);
75 this.infowindow = new google.maps.InfoWindow();
78 set_checkbox: function(record, checked)
80 /* Default: checked = true */
81 if (typeof checked === 'undefined')
85 /* The function accepts both records and their key */
86 switch (manifold.get_type(record)) {
91 /* XXX Test the key before ? */
92 key_value = record[this.key];
95 throw "Not implemented";
99 // we cannot directly edit html, since nothing but marker is displayed
100 //var checkbox_id = this.id('checkbox', this.id_from_key(this.key, key_value));
101 //checkbox_id = '#' + checkbox_id.replace(/\./g, '\\.');
102 //$(checkbox_id, this.table.fnGetNodes()).attr('checked', checked);
104 var dict_info = this.map_markers[unfold.escape_id(key_value).replace(/\\/g, '')];
107 // Update the marker content
108 dict_info.in_set = checked;
109 dict_info.marker.content = this.get_marker_content(dict_info.record, checked);
111 // Update opened infowindow
112 // XXX Factor this code
113 this.infowindow.close();
114 this.infowindow.open(this.map, dict_info.marker);
115 this.infowindow.setContent(dict_info.marker.content);
116 this.els('map-button').unbind('click').click(this, this._button_click);
118 //var button = this.checkbox(record, checked);
119 //this.el('checkbox', this.id_from_record(method, record)).html(button);
122 checkbox: function(record, checked)
124 if (typeof checked === 'undefined')
127 var method = manifold.query_store.find_analyzed_query(this.options.query_uuid).object;
128 var action = checked ? 'checked' : 'del';
130 action_class : checked ? 'ui-icon-circle-minus' : 'ui-icon-circle-plus',
131 action_message: checked ? 'Remove from slice' : 'Add to slice',
133 var button = this.load_template('template', ctx);
135 var id_record = this.id_from_record(method, record);
138 var id = this.id('checkbox', this.id_from_record(method, record));
139 return "<div id='" + id + "'>" + button + "</div>";
142 get_marker_content: function(record, checked)
144 return '<p><b>' + this.method + '</b>: ' + get_value(record['resource_hrn']) + '<br/><b>network</b>: ' + get_value(record['network'])+'</p>' + this.checkbox(record, checked);
149 new_record: function(record)
151 // get the coordinates
152 var latitude=get_value(record['latitude']);
153 var longitude=get_value(record['longitude']);
154 var hash = latitude + longitude;
156 // check to see if we've seen this hash before
157 if(this.coords[hash] == null) {
158 // get coordinate object
159 var myLatlng = new google.maps.LatLng(latitude, longitude);
160 // store an indicator that we've seen this point before
161 this.coords[hash] = 1;
163 // add some randomness to this point 1500 = 100 meters, 15000 = 10 meters
164 var lat = latitude + (Math.random() -.5) / 1500;
165 var lng = longitude + (Math.random() -.5) / 1500;
167 // get the coordinate object
168 var myLatlng = new google.maps.LatLng(lat, lng);
171 if (!(record['latitude'])) {
175 //jQuery(".map-button").click(button_click);
176 //if(jQuery.inArray(record, rows)>-1){
177 var marker = new google.maps.Marker({
179 title: get_value(record['hostname']),
180 // This should be done by the rendering
181 content: this.get_marker_content(record, false),
184 this.addInfoWindow(marker, this.map);
185 var key_value = (this.key in record) ? record[this.key] : null;
188 this.map_markers[unfold.escape_id(key_value).replace(/\\/g, '')] = {
198 addInfoWindow: function(marker, map)
201 google.maps.event.addListener(marker, 'click', function () {
203 self.infowindow.close();
205 self.infowindow.setContent(marker.content);// = new google.maps.InfoWindow({ content: marker.content });
206 self.infowindow.open(map, marker);
207 // onload of the infowindow on the map, bind a click on a button
208 google.maps.event.addListener(self.infowindow, 'domready', function() {
209 self.els('map-button').unbind('click').click(self, self._button_click);
210 // jQuery(".map-button").click({instance: instance_, infoWindow: object.infowindow}, button_click);
216 /*************************** QUERY HANDLER ****************************/
218 /*************************** RECORD HANDLER ***************************/
220 on_new_record: function(record)
222 if (this.received_all)
223 // update checkbox for record
224 this.set_checkbox(record);
226 // store for later update of checkboxes
227 this.in_set_buffer.push(record);
230 on_clear_records: function(record)
235 // Could be the default in parent
236 on_query_in_progress: function()
241 on_query_done: function()
243 if (this.received_all)
245 this.received_set = true;
248 on_field_state_changed: function(data)
250 switch(data.request) {
251 case FIELD_REQUEST_ADD:
252 case FIELD_REQUEST_ADD_RESET:
253 this.set_checkbox(data.value, true);
255 case FIELD_REQUEST_REMOVE:
256 case FIELD_REQUEST_REMOVE_RESET:
257 this.set_checkbox(data.value, false);
267 on_all_new_record: function(record)
269 this.new_record(record);
272 on_all_clear_records: function()
276 on_all_query_in_progress: function()
282 on_all_query_done: function()
287 $.each(this.map_markers, function (k, v) { markers.push(v.marker); });
289 this.markerCluster = new MarkerClusterer(this.map, markers, {zoomOnClick: false});
290 google.maps.event.addListener(this.markerCluster, "clusterclick", function (cluster) {
291 var cluster_markers = cluster.getMarkers();
292 var bounds = new google.maps.LatLngBounds();
296 * Firefox JS Error - replaced $.each by JQuery.each
298 jQuery.each(cluster_markers, function(i, marker){
299 bounds.extend(marker.getPosition());
302 //map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
303 this.map.fitBounds(bounds);
307 if (this.received_set) {
308 /* XXX needed ? XXX We uncheck all checkboxes ... */
309 //$("[id^='datatables-checkbox-" + this.options.plugin_uuid +"']").attr('checked', false);
311 /* ... and check the ones specified in the resource list */
312 $.each(this.in_set_buffer, function(i, record) {
313 self.set_checkbox(record, true);
318 this.received_all = true;
320 }, // on_all_query_done
322 /************************** PRIVATE METHODS ***************************/
324 _button_click: function(e)
328 var escaped_key = self.key_from_id($(this).parent().attr('id'), 'checkbox');
329 var info_dict = self.map_markers[escaped_key];
330 var in_set = info_dict.in_set;
331 var value = info_dict.value;
333 var escaped_key = self.map_markers[self.key_from_id($(this).parent().attr('id'), 'checkbox')]
334 manifold.raise_event(self.options.query_uuid, (in_set) ? SET_REMOVED : SET_ADD, value);
339 $.plugin('GoogleMaps', GoogleMaps);