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)
124 google.maps.event.addListener(marker, 'click', function () {
125 if(object.infowindow){
126 object.infowindow.close();
128 object.infowindow.setContent(marker.content);// = new google.maps.InfoWindow({ content: marker.content });
129 object.infowindow.open(map, marker);
130 // onload of the infowindow on the map, bind a click on a button
131 google.maps.event.addListener(object.infowindow, 'domready', function() {
132 jQuery('.map-button').unbind('click');
133 // jQuery(".map-button").click({instance: instance_, infoWindow: object.infowindow}, button_click);
138 set_checkbox: function(record)
140 // XXX urn should be replaced by the key
141 // XXX we should enforce that both queries have the same key !!
142 //checkbox_id = "#hazelnut-checkbox-" + object.options.plugin_uuid + "-" + unfold.escape_id(record[ELEMENT_KEY].replace(/\\/g, ''))
143 //$(checkbox_id, object.table.fnGetNodes()).attr('checked', true);
147 /*************************** QUERY HANDLER ****************************/
149 /*************************** RECORD HANDLER ***************************/
151 on_new_record: function(record)
153 if (this.received_all)
154 // update checkbox for record
155 this.set_checkbox(record);
157 // store for later update of checkboxes
158 this.in_set_buffer.push(record);
161 on_clear_records: function(record)
166 // Could be the default in parent
167 on_query_in_progress: function()
172 on_query_done: function()
174 if (this.received_all)
176 this.received_set = true;
181 on_all_new_record: function(record)
183 this.new_record(record);
186 on_all_clear_records: function()
190 on_all_query_in_progress: function()
196 on_all_query_done: function()
200 this.markerCluster = new MarkerClusterer(this.map, this.markers, {zoomOnClick: false});
201 google.maps.event.addListener(this.markerCluster, "clusterclick", function (cluster) {
202 var markers = cluster.getMarkers();
203 var bounds = new google.maps.LatLngBounds();
207 * Firefox JS Error - replaced $.each by JQuery.each
209 jQuery.each(markers, function(i, marker){
210 bounds.extend(marker.getPosition());
213 //map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
214 this.map.fitBounds(bounds);
218 if (this.received_set) {
219 /* XXX needed ? XXX We uncheck all checkboxes ... */
220 $("[id^='datatables-checkbox-" + this.options.plugin_uuid +"']").attr('checked', false);
222 /* ... and check the ones specified in the resource list */
223 $.each(this.in_set_buffer, function(i, record) {
224 self.set_checkbox(record);
229 this.received_all = true;
231 }, // on_all_query_done
233 /************************** PRIVATE METHODS ***************************/
235 _button_click: function(e)
237 var op_value=this.id.split("/");
238 if(op_value.length>0) {
239 var value = op_value[1];
240 manifold.raise_event(this.options.query_uuid, (op_value[0] == 'add')?SET_ADD:SET_REMOVED, value);
246 $.plugin('GoogleMaps', GoogleMaps);