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 // hrn --> { <li>, <input> }
36 this.elmt().on('show', this, this.on_show);
38 // $(window).unbind('Hazelnut');
40 var query = manifold.query_store.find_analyzed_query(this.options.query_uuid);
41 this.object = query.object;
43 var keys = manifold.metadata.get_key(this.object);
45 this.key = (keys && keys.length == 1) ? keys[0] : null;
47 //// Setup query and record handlers
48 // this query is the one about the slice itself
49 // event related to this query will trigger callbacks like on_new_record
50 this.listen_query(options.query_uuid);
51 // this one is the complete list of resources
52 // and will be bound to callbacks like on_all_new_record
53 this.listen_query(options.query_all_uuid, 'all');
55 /* GUI setup and event binding */
56 this.initialize_map();
61 on_show: function(e) {
62 // if (googlemap_debug) messages.debug("on_show");
63 messages.debug("on_show");
64 var googlemap = e.data;
65 google.maps.event.trigger(googlemap.map, 'resize');
70 /* GUI MANIPULATION */
72 initialize_map: function() {
73 this.markerCluster = null;
75 var center = new google.maps.LatLng(this.options.latitude, this.options.longitude);
77 zoom: this.options.zoom,
79 mapTypeId: google.maps.MapTypeId.ROADMAP,
82 var domid = this.options.plugin_uuid + '--' + 'googlemap';
83 var elmt = document.getElementById(domid);
84 if (googlemap_debug) messages.debug("gmap.initialize_map based on domid=" + domid + " elmt=" + elmt);
85 this.map = new google.maps.Map(elmt, myOptions);
86 this.infowindow = new google.maps.InfoWindow();
89 // xxx probably not the right place
90 // The function accepts both records and their key
91 record_hrn : function (record) {
93 switch (manifold.get_type(record)) {
98 if ( ! this.key in record ) return;
99 key_value = record[this.key];
102 throw "Not implemented";
105 return this.escape_id(key_value).replace(/\\/g, '');
108 // return { marker: gmap_marker, ul : <ul DOM> }
109 create_marker_struct: function (object,lat,lon) {
111 var dom = $("<p>").addClass("geo").append(object+"(s)");
112 var ul = $("<ul>").addClass("geo");
114 // add a gmap marker to the mix
115 var marker = new google.maps.Marker({
116 position: new google.maps.LatLng(lat, lon),
118 // gmap can deal with a DOM element but not a jquery object
121 return {marker:marker, ul:ul};
124 // add an entry in the marker <ul> tag for that record
125 // returns { checkbox : <input DOM> }
126 create_record_checkbox: function (record,ul,checked) {
127 var checkbox = $("<input>", {type:'checkbox', checked:checked, class:'geo'});
128 var hrn=this.record_hrn(record);
129 ul.append($("<li>").addClass("geo").append(checkbox).
130 append($("<span>").addClass("geo").append(hrn)));
132 // the callback for when a user clicks
133 // NOTE: this will *not* be called for changes done by program
134 checkbox.change( function (e) {
135 if (googlemap_debug) messages.debug("googlemap click handler checked= " + this.checked + " hrn=" + hrn);
136 manifold.raise_event (googlemap.options.query_uuid,
137 this.checked ? SET_ADD : SET_REMOVED, hrn);
142 // retrieve DOM checkbox and make sure it is checked/unchecked
143 set_checkbox: function(record, checked) {
144 var hrn=this.record_hrn (record);
145 if (! hrn) { messages.warning ("googlemap.set_checkbox: record has no hrn"); return; }
146 var checkbox_s = this.by_hrn [ hrn ];
147 if (! checkbox_s ) { messages.warning ("googlemap.set_checkbox: could not spot checkbox for hrn "+hrn); return; }
148 checkbox_s.checkbox.prop('checked',checked);
151 // this record is *in* the slice
152 new_record: function(record) {
153 if (googlemap_debug_detailed) messages.debug ("new_record");
154 if (!(record['latitude'])) return false;
156 // get the coordinates
157 var latitude=get_value(record['latitude']);
158 var longitude=get_value(record['longitude']);
159 var lat_lon = latitude + longitude;
161 // check if we've seen anything at that place already
162 // xxx might make sense to allow for some fuzziness,
163 // i.e. consider 2 places equal if not further away than 300m or so...
164 var marker_s = this.by_lat_lon [lat_lon];
165 if ( marker_s == null ) {
166 marker_s = this.create_marker_struct (this.object, latitude, longitude);
167 this.by_lat_lon [ lat_lon ] = marker_s;
168 this.arm_marker(marker_s.marker, this.map);
171 // now add a line for this resource in the marker
172 // xxx should compute checked here ?
173 // this is where the checkbox will be appended
175 var checkbox = this.create_record_checkbox (record, ul, false);
176 if ( ! this.key in record ) return;
177 var key_value = record[this.key];
178 var hrn = this.escape_id(key_value).replace(/\\/g, '');
181 // xxx Thierry sept 2013
182 // xxx actually we might have just used a domid-based scheme instead of the hash
183 // since at this point we only need to retrieve the checkbox from an hrn
184 // but I was not sure enough that extra needs would not show up so I kept this in place
185 // xxx not sure these are actually useful :
191 arm_marker: function(marker, map) {
192 if (googlemap_debug_detailed) messages.debug ("arm_marker content="+marker.content);
193 var googlemap = this;
194 google.maps.event.addListener(marker, 'click', function () {
195 googlemap.infowindow.close();
196 googlemap.infowindow.setContent(marker.content);
197 googlemap.infowindow.open(map, marker);
201 /*************************** QUERY HANDLER ****************************/
203 /*************************** RECORD HANDLER ***************************/
204 on_new_record: function(record) {
205 if (googlemap_debug_detailed) messages.debug("on_new_record");
206 if (this.received_all)
207 // update checkbox for record
208 this.set_checkbox(record, true);
210 // store for later update of checkboxes
211 this.in_set_backlog.push(record);
214 on_clear_records: function(record) {
215 if (googlemap_debug_detailed) messages.debug("on_clear_records");
218 // Could be the default in parent
219 on_query_in_progress: function() {
220 if (googlemap_debug) messages.debug("on_query_in_progress (spinning)");
224 on_query_done: function() {
225 if (googlemap_debug) messages.debug("on_query_done");
226 if (this.received_all) {
229 this.received_set = true;
232 on_field_state_changed: function(data) {
233 if (googlemap_debug_detailed) messages.debug("on_field_state_changed");
234 switch(data.request) {
235 case FIELD_REQUEST_ADD:
236 case FIELD_REQUEST_ADD_RESET:
237 this.set_checkbox(data.value, true);
239 case FIELD_REQUEST_REMOVE:
240 case FIELD_REQUEST_REMOVE_RESET:
241 this.set_checkbox(data.value, false);
251 on_all_new_record: function(record) {
252 if (googlemap_debug_detailed) messages.debug("on_all_new_record");
253 this.new_record(record);
256 on_all_clear_records: function() {
257 if (googlemap_debug) messages.debug("on_all_clear_records");
260 on_all_query_in_progress: function() {
261 if (googlemap_debug) messages.debug("on_all_query_in_progress (spinning)");
266 on_all_query_done: function() {
267 if (googlemap_debug) messages.debug("on_all_query_done");
271 $.each(this.by_lat_lon, function (k, s) { markers.push(s.marker); });
272 this.markerCluster = new MarkerClusterer(this.map, markers, {zoomOnClick: false});
273 google.maps.event.addListener(this.markerCluster, "clusterclick", function (cluster) {
274 var cluster_markers = cluster.getMarkers();
275 var bounds = new google.maps.LatLngBounds();
276 $.each(cluster_markers, function(i, marker){
277 bounds.extend(marker.getPosition());
279 //map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
280 this.map.fitBounds(bounds);
283 var googlemap = this;
284 if (this.received_set) {
285 /* ... and check the ones specified in the resource list */
286 $.each(this.in_set_backlog, function(i, record) {
287 googlemap.set_checkbox(record, true);
290 googlemap.in_set_backlog = [];
292 if (googlemap_debug) messages.debug("unspinning");
295 this.received_all = true;
297 } // on_all_query_done
299 /************************** PRIVATE METHODS ***************************/
301 $.plugin('GoogleMap', GoogleMap);