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 // events that happen in the once-per-view range
12 googlemap_debug=false;
13 // more on a on-per-record basis
14 googlemap_debug_detailed=false;
18 var GoogleMap = Plugin.extend({
20 init: function(options, element) {
21 this._super(options, element);
23 /* Member variables */
25 this.received_all = false;
26 this.received_set = false;
27 this.in_set_backlog = [];
29 // we keep a couple of global hashes
30 // lat_lon --> { marker, <ul> }
31 // id --> { <li>, <input> }
36 this.elmt().on('show', this, this.on_show);
38 var query = manifold.query_store.find_analyzed_query(this.options.query_uuid);
39 this.object = query.object;
41 var keys = manifold.metadata.get_key(this.object);
43 this.key = (keys && keys.length == 1) ? keys[0] : null;
46 // as of nov. 28 2013 we have here this.key='urn', but in any place where
47 // the code tries to access record[this.key] the records only have
48 // keys=type,hrn,network_hrn,hostname
49 // so for now we force using hrn instead
50 // as soon as record have their primary key set this line can be removed
51 // see also same hack in querytable
52 this.key= (this.key == 'urn') ? 'hrn' : this.key;
54 //// Setup query and record handlers
55 // this query is the one about the slice itself
56 // event related to this query will trigger callbacks like on_new_record
57 this.listen_query(options.query_uuid);
58 // this one is the complete list of resources
59 // and will be bound to callbacks like on_all_new_record
60 this.listen_query(options.query_all_uuid, 'all');
62 /* GUI setup and event binding */
63 this.initialize_map();
68 on_show: function(e) {
69 if (googlemap_debug) messages.debug("googlemap.on_show");
70 var googlemap = e.data;
71 google.maps.event.trigger(googlemap.map, 'resize');
76 /* GUI MANIPULATION */
78 initialize_map: function() {
79 this.markerCluster = null;
81 var center = new google.maps.LatLng(this.options.latitude, this.options.longitude);
83 zoom: this.options.zoom,
86 mapTypeId: google.maps.MapTypeId.ROADMAP,
89 var domid = this.options.plugin_uuid + '--' + 'googlemap';
90 var elmt = document.getElementById(domid);
91 if (googlemap_debug) messages.debug("gmap.initialize_map based on domid=" + domid + " elmt=" + elmt);
92 this.map = new google.maps.Map(elmt, myOptions);
93 this.infowindow = new google.maps.InfoWindow();
96 // The function accepts both records and their id
97 // record.key points to the name of the primary key for this record
98 // typically this is 'urn'
99 record_id : function (input) {
101 switch (manifold.get_type(input)) {
106 if ( ! this.key in input ) return;
107 id = input[this.key];
110 throw "googlemap.record_id: not implemented";
116 // return { marker: gmap_marker, ul : <ul DOM> }
117 create_marker_struct: function (object,lat,lon) {
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
129 return {marker:marker, ul:ul};
132 // given an input <ul> element, this method inserts a <li> with embedded checkbox
133 // for displaying/selecting the resource corresponding to the input record
134 // returns the created <input> element for further checkbox manipulation
135 create_record_checkbox: function (record,ul,checked) {
136 var checkbox = $("<input>", {type:'checkbox', checked:checked, class:'geo'});
137 var id=this.record_id(record);
138 // use hrn as far as possible for displaying
139 var label= ('hrn' in record) ? record.hrn : id;
140 ul.append($("<li>").addClass("geo").append(checkbox).
141 append($("<span>").addClass("geo").append(label)));
143 // the callback for when a user clicks
144 // NOTE: this will *not* be called for changes done by program
145 checkbox.change( function (e) {
146 manifold.raise_event (googlemap.options.query_uuid, this.checked ? SET_ADD : SET_REMOVED, id);
151 warning: function (record,message) {
152 try {messages.warning (message+" -- hostname="+record.hostname); }
153 catch (err) {messages.warning (message); }
156 // retrieve DOM checkbox and make sure it is checked/unchecked
157 set_checkbox: function(record, checked) {
158 var id=this.record_id (record);
160 this.warning (record, "googlemap.set_checkbox: record has no id");
163 var checkbox = this.by_id [ id ];
165 this.warning (record, "googlemap.set_checkbox: checkbox not found");
168 checkbox.prop('checked',checked);
171 // this record is *in* the slice
172 new_record: function(record) {
173 if (googlemap_debug_detailed) messages.debug ("new_record");
174 if (!(record['latitude'])) return false;
176 // get the coordinates
177 var latitude=unfold.get_value(record['latitude']);
178 var longitude=unfold.get_value(record['longitude']);
179 var lat_lon = latitude + longitude;
181 // check if we've seen anything at that place already
182 // xxx might make sense to allow for some fuzziness,
183 // i.e. consider 2 places equal if not further away than 300m or so...
184 var marker_s = this.by_lat_lon [lat_lon];
185 if ( marker_s == null ) {
186 marker_s = this.create_marker_struct (this.object, latitude, longitude);
187 this.by_lat_lon [ lat_lon ] = marker_s;
188 this.arm_marker(marker_s.marker, this.map);
191 // now add a line for this resource in the marker
192 // xxx should compute checked here ?
193 // this is where the checkbox will be appended
195 var checkbox = this.create_record_checkbox (record, ul, false);
196 var id=this.record_id(record);
197 // used to keep a dict here, but only checkbox is required
198 this.by_id[id] = checkbox;
201 arm_marker: function(marker, map) {
202 if (googlemap_debug_detailed) messages.debug ("arm_marker content="+marker.content);
203 var googlemap = this;
204 google.maps.event.addListener(marker, 'click', function () {
205 googlemap.infowindow.close();
206 googlemap.infowindow.setContent(marker.content);
207 googlemap.infowindow.open(map, marker);
211 /*************************** QUERY HANDLER ****************************/
213 /*************************** RECORD HANDLER ***************************/
214 on_new_record: function(record) {
215 if (googlemap_debug_detailed) messages.debug("on_new_record");
216 if (this.received_all)
217 // update checkbox for record
218 this.set_checkbox(record, true);
220 // store for later update of checkboxes
221 this.in_set_backlog.push(record);
224 on_clear_records: function(record) {
225 if (googlemap_debug_detailed) messages.debug("on_clear_records");
228 // Could be the default in parent
229 on_query_in_progress: function() {
230 if (googlemap_debug) messages.debug("on_query_in_progress (spinning)");
234 on_query_done: function() {
235 if (googlemap_debug) messages.debug("on_query_done");
236 if (this.received_all) {
239 this.received_set = true;
242 on_field_state_changed: function(data) {
243 if (googlemap_debug_detailed) messages.debug("on_field_state_changed");
244 switch(data.request) {
245 case FIELD_REQUEST_ADD:
246 case FIELD_REQUEST_ADD_RESET:
247 this.set_checkbox(data.value, true);
249 case FIELD_REQUEST_REMOVE:
250 case FIELD_REQUEST_REMOVE_RESET:
251 this.set_checkbox(data.value, false);
261 on_all_new_record: function(record) {
262 if (googlemap_debug_detailed) messages.debug("on_all_new_record");
263 this.new_record(record);
266 on_all_clear_records: function() {
267 if (googlemap_debug) messages.debug("on_all_clear_records");
270 on_all_query_in_progress: function() {
271 if (googlemap_debug) messages.debug("on_all_query_in_progress (spinning)");
276 on_all_query_done: function() {
277 if (googlemap_debug) messages.debug("on_all_query_done");
281 $.each(this.by_lat_lon, function (k, s) { markers.push(s.marker); });
282 this.markerCluster = new MarkerClusterer(this.map, markers, {zoomOnClick: false});
283 google.maps.event.addListener(this.markerCluster, "clusterclick", function (cluster) {
284 var cluster_markers = cluster.getMarkers();
285 var bounds = new google.maps.LatLngBounds();
286 $.each(cluster_markers, function(i, marker){
287 bounds.extend(marker.getPosition());
289 //map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
290 this.map.fitBounds(bounds);
293 var googlemap = this;
294 if (this.received_set) {
295 /* ... and check the ones specified in the resource list */
296 $.each(this.in_set_backlog, function(i, record) {
297 googlemap.set_checkbox(record, true);
300 googlemap.in_set_backlog = [];
302 if (googlemap_debug) messages.debug("unspinning");
305 this.received_all = true;
307 } // on_all_query_done
309 /************************** PRIVATE METHODS ***************************/
311 $.plugin('GoogleMap', GoogleMap);