2 * Description: display a query result in a Google map
3 * Copyright (c) 2012-2013 UPMC Sorbonne Universite - INRIA
9 var GoogleMaps = Plugin.extend({
11 init: function(options, element)
13 this._super(options, element);
15 /* Member variables */
17 this.received_all = false;
18 this.received_set = false;
19 this.in_set_buffer = Array();
22 this.$element.on('show.Datatables', this.on_show);
24 // $(window).unbind('Hazelnut');
26 /* Setup query and record handlers */
27 this.listen_query(options.query_uuid);
28 this.listen_query(options.query_all_uuid, 'all');
30 /* GUI setup and event binding */
31 this.initialize_map();
38 google.maps.event.trigger(map, 'resize');
43 /* GUI MANIPULATION */
47 initialize_map: function()
50 this.markerCluster = null;
52 this.coords = new Array();
54 var myLatlng = new google.maps.LatLng(this.options.latitude, this.options.longitude);
56 zoom: this.options.zoom,
58 mapTypeId: google.maps.MapTypeId.ROADMAP
61 this.map = new google.maps.Map(document.getElementById("map"), myOptions);
62 this.infowindow = new google.maps.InfoWindow();
67 new_record: function(record)
69 // get the coordinates
70 var latitude=get_value(record['latitude']);
71 var longitude=get_value(record['longitude']);
72 var hash = latitude + longitude;
74 // check to see if we've seen this hash before
75 if(this.coords[hash] == null) {
76 // get coordinate object
77 var myLatlng = new google.maps.LatLng(latitude, longitude);
78 // store an indicator that we've seen this point before
79 this.coords[hash] = 1;
81 // add some randomness to this point 1500 = 100 meters, 15000 = 10 meters
82 var lat = latitude + (Math.random() -.5) / 1500;
83 var lng = longitude + (Math.random() -.5) / 1500;
85 // get the coordinate object
86 var myLatlng = new google.maps.LatLng(lat, lng);
88 // If the node is attached to the slice, action will be Remove; else action will be add to slice
89 if (typeof(record['sliver']) != 'undefined') {
90 data.current_resources.push(record['urn']);
92 action_class="ui-icon-circle-minus";
93 action_message="Remove from slice";
96 action_class="ui-icon-circle-plus";
97 action_message="Add to slice";
100 if (!(record['latitude'])) {
104 //jQuery(".map-button").click(button_click);
105 //if(jQuery.inArray(record, rows)>-1){
106 var marker = new google.maps.Marker({
108 title: get_value(record['hostname']),
109 // This should be done by the rendering
110 content: '<p>Agent: ' + get_value(record['ip']) + ' (' + get_value(record['resource_hrn']) + ')<br/>Platform: ' + get_value(record['platform'])+'</p>' +
111 '<div class="map-button" id="'+action+'/'+get_value(record['resource_hrn'])+'" style="cursor:pointer;">'+
112 '<span class="ui-icon '+action_class+'" style="clear:both;float:left;"></span>'+action_message+
116 this.addInfoWindow(marker, this.map);
117 this.markers.push(marker);
122 addInfoWindow: function(marker, map)
125 google.maps.event.addListener(marker, 'click', function () {
127 self.infowindow.close();
129 self.infowindow.setContent(marker.content);// = new google.maps.InfoWindow({ content: marker.content });
130 self.infowindow.open(map, marker);
131 // onload of the infowindow on the map, bind a click on a button
132 google.maps.event.addListener(self.infowindow, 'domready', function() {
133 jQuery('.map-button').unbind('click');
134 // jQuery(".map-button").click({instance: instance_, infoWindow: object.infowindow}, button_click);
139 set_checkbox: function(record)
141 // XXX urn should be replaced by the key
142 // XXX we should enforce that both queries have the same key !!
143 //checkbox_id = "#hazelnut-checkbox-" + object.options.plugin_uuid + "-" + unfold.escape_id(record[ELEMENT_KEY].replace(/\\/g, ''))
144 //$(checkbox_id, object.table.fnGetNodes()).attr('checked', true);
148 /*************************** QUERY HANDLER ****************************/
150 /*************************** RECORD HANDLER ***************************/
152 on_new_record: function(record)
154 if (this.received_all)
155 // update checkbox for record
156 this.set_checkbox(record);
158 // store for later update of checkboxes
159 this.in_set_buffer.push(record);
162 on_clear_records: function(record)
167 // Could be the default in parent
168 on_query_in_progress: function()
173 on_query_done: function()
175 if (this.received_all)
177 this.received_set = true;
182 on_all_new_record: function(record)
184 this.new_record(record);
187 on_all_clear_records: function()
191 on_all_query_in_progress: function()
197 on_all_query_done: function()
201 this.markerCluster = new MarkerClusterer(this.map, this.markers, {zoomOnClick: false});
202 google.maps.event.addListener(this.markerCluster, "clusterclick", function (cluster) {
203 var markers = cluster.getMarkers();
204 var bounds = new google.maps.LatLngBounds();
208 * Firefox JS Error - replaced $.each by JQuery.each
210 jQuery.each(markers, function(i, marker){
211 bounds.extend(marker.getPosition());
214 //map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
215 this.map.fitBounds(bounds);
219 if (this.received_set) {
220 /* XXX needed ? XXX We uncheck all checkboxes ... */
221 $("[id^='datatables-checkbox-" + this.options.plugin_uuid +"']").attr('checked', false);
223 /* ... and check the ones specified in the resource list */
224 $.each(this.in_set_buffer, function(i, record) {
225 self.set_checkbox(record);
230 this.received_all = true;
232 }, // on_all_query_done
234 /************************** PRIVATE METHODS ***************************/
236 _button_click: function(e)
238 var op_value=this.id.split("/");
239 if(op_value.length>0) {
240 var value = op_value[1];
241 manifold.raise_event(this.options.query_uuid, (op_value[0] == 'add')?SET_ADD:SET_REMOVED, value);
247 $.plugin('GoogleMaps', GoogleMaps);