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 does not have the focus
11 GOOGLEMAP_BGCOLOR_RESET = 0;
12 GOOGLEMAP_BGCOLOR_ADDED = 1;
13 GOOGLEMAP_BGCOLOR_REMOVED = 2;
17 // events that happen in the once-per-view range
21 // this now should be obsolete, rather use plugin_debug in plugin.js
22 // more on a on-per-record basis
26 var GoogleMap = Plugin.extend({
28 /**************************************************************************
30 **************************************************************************/
32 init: function(options, element)
34 this._super(options, element);
36 /* Member variables */
38 /* we keep a couple of global hashes
39 * lat_lon --> { marker, <ul> }
40 * id --> { <li>, <input> }
43 /* locating checkboxes by DOM selectors might be abstruse, as we cannot safely assume
44 * all the items will belong under the toplevel <div>
49 this.markers = Array();
52 this.elmt().on('show', this, this.on_show);
53 this.elmt().on('shown.bs.tab', this, this.on_show);
54 this.elmt().on('resize', this, this.on_resize);
56 var query = manifold.query_store.find_analyzed_query(this.options.query_uuid);
57 this.object = query.object;
59 /* see querytable.js for an explanation */
60 var keys = manifold.metadata.get_key(this.object);
61 this.canonical_key = (keys && keys.length == 1) ? keys[0] : undefined;
62 this.init_key = this.options.init_key;
63 this.init_key = this.init_key || this.canonical_key;
66 if ( ! this.init_key )
67 messages.warning ("QueryTable : cannot find init_key");
68 if ( ! this.canonical_key )
69 messages.warning ("QueryTable : cannot find canonical_key");
71 messages.debug("googlemap: canonical_key="+this.canonical_key+" init_key="+this.init_key);
73 this.listen_query(options.query_uuid);
75 /* GUI setup and event binding */
76 this.initialize_map();
80 /**************************************************************************
82 **************************************************************************/
84 on_show: function(e) {
85 if (debug) messages.debug("googlemap.on_show");
86 var googlemap = e.data;
87 google.maps.event.trigger(googlemap.map, 'resize');
90 /**************************************************************************
92 **************************************************************************/
94 initialize_map: function()
96 this.markerCluster = null;
97 //create empty LatLngBounds object in order to automatically center the map on the displayed objects
98 this.bounds = new google.maps.LatLngBounds();
99 var center = new google.maps.LatLng(this.options.latitude, this.options.longitude);
102 zoom: this.options.zoom,
105 mapTypeId: google.maps.MapTypeId.ROADMAP,
108 var domid = this.id('googlemap');
109 var elmt = document.getElementById(domid);
110 this.map = new google.maps.Map(elmt, myOptions);
111 this.infowindow = new google.maps.InfoWindow();
115 // return { marker: gmap_marker, ul : <ul DOM> }
116 create_marker_struct: function(object, lat, lon)
118 /* the DOM fragment */
119 var dom = $("<p>").addClass("geo").append(object+"(s)");
120 var ul = $("<ul>").addClass("geo");
122 /* add a gmap marker to the mix */
123 var marker = new google.maps.Marker({
124 position: new google.maps.LatLng(lat, lon),
126 /* gmap can deal with a DOM element but not a jquery object */
130 //extend the bounds to include each marker's position
131 this.bounds.extend(marker.position);
132 return { marker: marker, ul: ul };
135 /* given an input <ul> element, this method inserts a <li> with embedded checkbox
136 * for displaying/selecting the resource corresponding to the input record
137 * returns the created <input> element for further checkbox manipulation
139 create_record_checkbox: function (record, ul, checked)
143 var checkbox = $("<input>", {type:'checkbox', checked:checked, class:'geo'});
144 var id = record[this.canonical_key];
145 var init_id = record[this.init_key];
147 // xxx use init_key to find out label - or should we explicitly accept an incoming label_key ?
150 key = this.canonical_key;
151 key_value = manifold.record_get_value(record, key);
153 ul.append($("<li>").addClass("geo")
154 .append($('<div>') // .addId(this.id_from_key(key, key_value))
156 .append($("<span>").addClass("geo")
162 // XXX STATE / BACKGROUND
164 // hash by id and by init_id
165 this.by_id[id]=checkbox;
166 this.by_init_id[init_id] = checkbox;
168 /* the callback for when a user clicks
169 * NOTE: this will *not* be called for changes done by program
172 checkbox.change( function (e) {
173 manifold.raise_event (self.options.query_uuid, this.checked ? SET_ADD : SET_REMOVED, id);
178 set_checkbox_from_record_key: function (record_key, checked)
180 if (checked === undefined) checked = true;
182 var checkbox = this.by_init_id [record_key];
184 console.log("googlemap.set_checkbox_from_record - not found " + record_key);
186 checkbox.prop('checked', checked);
190 set_checkbox_from_data: function(id, checked)
192 var checkbox = this.by_id[id];
194 console.log("googlemap.set_checkbox_from_data - id not found " + id);
195 checkbox.prop('checked', checked);
198 set_bgcolor: function(key_value, class_name)
200 var elt = $(document.getElementById(this.id_from_key(this.canonical_key, key_value)))
201 if (class_name == GOOGLEMAP_BGCOLOR_RESET)
202 elt.removeClass('added removed');
204 elt.addClass((class_name == GOOGLEMAP_BGCOLOR_ADDED ? 'added' : 'removed'));
209 * Populates both this.by_lat_lon and this.arm_marker arrays
211 new_record: function(record)
215 if (!(record['latitude']))
218 /* get the coordinates*/
219 var latitude = unfold.get_value(record['latitude']);
220 var longitude = unfold.get_value(record['longitude']);
221 var lat_lon = latitude + longitude;
223 // check if we've seen anything at that place already
224 // xxx might make sense to allow for some fuzziness,
225 // i.e. consider 2 places equal if not further away than 300m or so...
226 var marker_s = this.by_lat_lon[lat_lon];
227 if ( marker_s == null ) {
228 marker_s = this.create_marker_struct(this.object, latitude, longitude);
229 this.by_lat_lon[lat_lon] = marker_s;
230 this.arm_marker(marker_s.marker, this.map);
233 /* Add key to the marker */
234 record_key = manifold.record_get_value(record, this.canonical_key);
235 marker_s.keys.push(record_key);
237 // now add a line for this resource in the marker
238 // xxx should compute checked here ?
239 // this is where the checkbox will be appended
240 var ul = marker_s.ul;
241 var checkbox = this.create_record_checkbox(record, ul, false);
244 arm_marker: function(marker, map)
247 google.maps.event.addListener(marker, 'click', function () {
248 self.infowindow.close();
249 self.infowindow.setContent(marker.content);
250 self.infowindow.open(map, marker);
254 clear_map: function()
259 filter_map: function()
264 /* Loop on every marker and sets it visible */
265 $.each(this.markers, function(i, marker) {
266 /* For a marker to be visible, at least one of its keys has to
269 $.each(marker.keys, function(j, key) {
270 visible |= manifold.query_store.get_record_state(self.options.query_uuid, key, STATE_VISIBLE);
272 marker.setVisible(visible);
276 redraw_map: function()
278 // Let's clear the table and only add lines that are visible
282 /* Add records to internal hash structure */
283 var record_keys = [];
284 manifold.query_store.iter_records(this.options.query_uuid, function (record_key, record) {
285 self.new_record(record);
286 record_keys.push(record_key);
288 this.table.fnAddData(lines);
290 /* Add markers to cluster */
291 this.markers = Array();
292 $.each(this.by_lat_lon, function (k, s) {
293 this.markers.push(s.marker);
296 this.markerCluster = new MarkerClusterer(this.map, this.markers, {zoomOnClick: false});
297 google.maps.event.addListener(this.markerCluster, "clusterclick", function (cluster) {
298 var cluster_markers = cluster.getMarkers();
299 var bounds = new google.maps.LatLngBounds();
300 $.each(cluster_markers, function(i, marker){
301 bounds.extend(marker.getPosition());
303 //map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
304 this.map.fitBounds(bounds);
306 //now fit the map to the bounds
307 this.map.fitBounds(this.bounds);
308 // Fix the zoom of fitBounds function, it's too close when there is only 1 marker
309 if (this.markers.length==1) {
310 this.map.setZoom(this.map.getZoom()-4);
313 /* Set checkbox and background color */
314 $.each(record_keys, function(i, record_key) {
315 var state = manifold.query_store.get_record_state(self.options.query_uuid, record_key, STATE_SET);
316 var warnings = manifold.query_store.get_record_state(self.options.query_uuid, record_key, STATE_WARNINGS);
318 // XXX The row and checkbox still does not exists !!!!
320 case STATE_SET_IN_SUCCESS:
321 case STATE_SET_OUT_FAILURE:
322 self.set_checkbox_from_record_key(record_key, true);
325 case STATE_SET_OUT_SUCCESS:
326 case STATE_SET_IN_FAILURE:
328 case STATE_SET_IN_PENDING:
329 self.set_checkbox_from_record_key(record_key, true);
330 self.set_bgcolor(record_key, GOOGLEMAP_BGCOLOR_ADDED);
332 case STATE_SET_OUT_PENDING:
333 self.set_bgcolor(record_key, GOOGLEMAP_BGCOLOR_REMOVED);
336 self.change_status(record_key, warnings); // XXX will retrieve status again
340 /**************************************************************************
342 **************************************************************************/
344 on_filter_added: function(filter)
349 on_filter_removed: function(filter)
354 on_filter_clear: function()
359 on_query_in_progress: function()
364 on_query_done: function()
370 on_field_state_changed: function(data)
372 switch(data.request) {
373 case FIELD_REQUEST_ADD:
374 case FIELD_REQUEST_ADD_RESET:
375 this.set_checkbox_from_data(data.value, true);
377 case FIELD_REQUEST_REMOVE:
378 case FIELD_REQUEST_REMOVE_RESET:
379 this.set_checkbox_from_data(data.value, false);
388 $.plugin('GoogleMap', GoogleMap);