1 | var mapEnabled = true; // variable to detect when map-scripts have been included into document_scripts.js and classifier_scripts.js
|
---|
2 |
|
---|
3 | //var newLat, newLng = 0;
|
---|
4 | var _docList = new Array();
|
---|
5 | _docList.ids = new Array();
|
---|
6 | _docList.getDocByIndex = function(index)
|
---|
7 | {
|
---|
8 | return _docList[_docList.ids[index]];
|
---|
9 | };
|
---|
10 |
|
---|
11 | var _map;
|
---|
12 | var _intervalHandle;
|
---|
13 | var _baseURL = document.URL.substring(0, document.URL.indexOf("?") + 1);
|
---|
14 | var _retrievedClassifiers = new Array();
|
---|
15 | var _preventLoopingSingleMarker = false;
|
---|
16 | var _searchRunning = false;
|
---|
17 | var _nearbyDocs = new Array();
|
---|
18 | var _scrollThroughDefault = true;
|
---|
19 |
|
---|
20 | function initializeMapScripts()
|
---|
21 | {
|
---|
22 | setUpMap();
|
---|
23 |
|
---|
24 | var jsonNodeDiv = $("#jsonNodes");
|
---|
25 | if(jsonNodeDiv.length)
|
---|
26 | {
|
---|
27 | //console.log("@@@ JSON node div html: " + jsonNodeDiv.html());
|
---|
28 | var jsonNodes = eval(jsonNodeDiv.html());
|
---|
29 |
|
---|
30 | if(jsonNodes && jsonNodes.length > 0)
|
---|
31 | {
|
---|
32 | for(var i = 0; i < jsonNodes.length; i++)
|
---|
33 | {
|
---|
34 | _docList[jsonNodes[i].nodeID] = jsonNodes[i];
|
---|
35 | _docList.ids.push(jsonNodes[i].nodeID);
|
---|
36 | createMarkers(jsonNodes[i], true);
|
---|
37 | }
|
---|
38 | updateMap();
|
---|
39 | }
|
---|
40 | else
|
---|
41 | {
|
---|
42 | //hiding the map
|
---|
43 | //$("#map_canvas").css({visibility:"hidden", height:"0px"});
|
---|
44 | $("#map_canvas").css({visibility:"hidden"}); // if you comment this out, add a log message saying "suppressing hiding map"
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | _docList.loopIndex = 0;
|
---|
49 |
|
---|
50 | if(_docList.ids.length > 1)
|
---|
51 | {
|
---|
52 | var startStopCheckbox = $("<input>", {"type": "checkbox", "checked": _scrollThroughDefault, "id": "scrollCheckbox"});
|
---|
53 | startStopCheckbox.click(function()
|
---|
54 | {
|
---|
55 | // http://stackoverflow.com/questions/901712/how-to-check-if-a-checkbox-is-checked-in-jquery
|
---|
56 | // http://stackoverflow.com/questions/5270689/attrchecked-checked-does-not-work
|
---|
57 |
|
---|
58 | if($('#scrollCheckbox').is(':checked')) // OR: if(document.getElementById('scrollCheckbox').checked)
|
---|
59 | {
|
---|
60 | if(_intervalHandle == null)
|
---|
61 | {
|
---|
62 | _intervalHandle = setInterval(loopThroughMarkers, 2000);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | else
|
---|
66 | {
|
---|
67 | clearInterval(_intervalHandle);
|
---|
68 | _intervalHandle = null;
|
---|
69 | }
|
---|
70 | });
|
---|
71 |
|
---|
72 | var label = $("<span>Scroll through places</span>");
|
---|
73 | var container = $("<div>", {"class": "ui-widget-header ui-corner-all", "style": "clear:right; float:right; padding:0px 5px 3px 0px;"});
|
---|
74 | container.append(startStopCheckbox);
|
---|
75 | container.append(label);
|
---|
76 |
|
---|
77 | $(container).insertAfter("#map_canvas");
|
---|
78 |
|
---|
79 | if (_scrollThroughDefault) {
|
---|
80 | _intervalHandle = setInterval(loopThroughMarkers, 2000);
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | function setUpMap()
|
---|
87 | {
|
---|
88 | //alert("@@@@in map-scripts::setUpMap()");
|
---|
89 |
|
---|
90 | var myOptions =
|
---|
91 | {
|
---|
92 | zoom: 2,
|
---|
93 | center: new google.maps.LatLng(0, 0),
|
---|
94 | mapTypeId: google.maps.MapTypeId.HYBRID
|
---|
95 | };
|
---|
96 | var $map_canvas = $("#map_canvas");
|
---|
97 |
|
---|
98 | if ($map_canvas.length > 0) {
|
---|
99 | _map = new google.maps.Map($map_canvas[0], myOptions);
|
---|
100 | google.maps.event.addListener(_map, 'bounds_changed', performSearchForMarkers);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | function performSearchForMarkers()
|
---|
105 | {
|
---|
106 | if(_searchRunning)
|
---|
107 | {
|
---|
108 | return;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 |
|
---|
113 | _searchRunning = true;
|
---|
114 |
|
---|
115 | var bounds = _map.getBounds();
|
---|
116 |
|
---|
117 | var neLat = bounds.getNorthEast().lat();
|
---|
118 | var neLng = bounds.getNorthEast().lng();
|
---|
119 | var swLat = bounds.getSouthWest().lat();
|
---|
120 | var swLng = bounds.getSouthWest().lng();
|
---|
121 |
|
---|
122 | var latDistance = neLat - swLat;
|
---|
123 | var lngDistance = neLng - swLng;
|
---|
124 |
|
---|
125 | //Check which increment to use for latitude (i.e. 0.001, 0.01, 0.1 or 1 degree increments)
|
---|
126 | var latDelta;
|
---|
127 | var latPrecision;
|
---|
128 | for(var i = 3; i >= 0; i--)
|
---|
129 | {
|
---|
130 | latDelta = (1 / Math.pow(10, i));
|
---|
131 | if((latDistance / latDelta) <= 5 || latDelta == 1)
|
---|
132 | {
|
---|
133 | latPrecision = i;
|
---|
134 | break;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | //Check which increment to use for longitude (i.e. 0.001, 0.01, 0.1 or 1 degree increments)
|
---|
139 | var lngDelta;
|
---|
140 | for(var i = 3; i >= 0; i--)
|
---|
141 | {
|
---|
142 | lngDelta = (1 / Math.pow(10, i));
|
---|
143 | if((lngDistance / lngDelta) <= 5 || lngDelta == 1)
|
---|
144 | {
|
---|
145 | lngPrecision = i;
|
---|
146 | break;
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | if(latDelta == 0.1){latDelta = 1; latPrecision = 0; }
|
---|
151 | if(lngDelta == 0.1){lngDelta = 1; lngPrecision = 0; }
|
---|
152 |
|
---|
153 | /*
|
---|
154 | var query = "";
|
---|
155 | for(var i = 0; i <= Math.floor(latDistance / latDelta) + 1; i++)
|
---|
156 | {
|
---|
157 | for(var j = 0; j <= Math.floor(lngDistance / lngDelta) + 1; j++)
|
---|
158 | {
|
---|
159 | //Some necessary variables
|
---|
160 | var newLat = neLat - (latDelta * i);
|
---|
161 | var newLatString = "" + newLat;
|
---|
162 | var newLatTrunc;
|
---|
163 | if(newLat < 0){newLatTrunc = Math.ceil(newLat);}
|
---|
164 | else{newLatTrunc = Math.floor(newLat);}
|
---|
165 |
|
---|
166 | var newLng = neLng - (lngDelta * j);
|
---|
167 | var newLngString = "" + newLng;
|
---|
168 | var newLngTrunc;
|
---|
169 | if(newLng < 0){newLngTrunc = Math.ceil(newLng);}
|
---|
170 | else{newLngTrunc = Math.floor(newLng);}
|
---|
171 |
|
---|
172 | //Construct query
|
---|
173 | query += "(";
|
---|
174 | query += "LA:" + coordToAbsDirected(newLatTrunc, "lat");
|
---|
175 | if(latDelta != 1)
|
---|
176 | {
|
---|
177 | query += "+AND+";
|
---|
178 | query += "LA:" + newLatString.substring(newLatString.indexOf(".") + 1, newLatString.indexOf(".") + latPrecision + 1);
|
---|
179 | }
|
---|
180 | query += "+AND+";
|
---|
181 | query += "LN:" + coordToAbsDirected(newLngTrunc, "lng");
|
---|
182 | if(lngDelta != 1)
|
---|
183 | {
|
---|
184 | query += "+AND+";
|
---|
185 | query += "LN:" + newLngString.substring(newLngString.indexOf(".") + 1, newLngString.indexOf(".") + lngPrecision + 1);
|
---|
186 | }
|
---|
187 | query += ")";
|
---|
188 |
|
---|
189 | if(i != (Math.floor(latDistance / latDelta) + 1) || j != (Math.floor(lngDistance / lngDelta) + 1)){ query += "+OR+"; }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | */
|
---|
193 | var query = "";
|
---|
194 | var iMax = Math.floor(latDistance / latDelta) + 1;
|
---|
195 | var jMax = Math.floor(lngDistance / lngDelta) + 1;;
|
---|
196 | for(var i = 0; i <= iMax; i++) //for(var i = 0; i <= Math.floor(latDistance / latDelta) + 1; i++)
|
---|
197 | {
|
---|
198 | for(var j = 0; j <= jMax; j++) //for(var j = 0; j <= Math.floor(lngDistance / lngDelta) + 1; j++)
|
---|
199 | {
|
---|
200 | //Some necessary variables
|
---|
201 | var newLat = neLat - (latDelta * i);
|
---|
202 | var newLatString = "" + newLat;
|
---|
203 | var newLatTrunc;
|
---|
204 | if(newLat < 0){newLatTrunc = Math.ceil(newLat);}
|
---|
205 | else{newLatTrunc = Math.floor(newLat);}
|
---|
206 |
|
---|
207 | var newLng = neLng - (lngDelta * j);
|
---|
208 | var newLngString = "" + newLng;
|
---|
209 | var newLngTrunc;
|
---|
210 | if(newLng < 0){newLngTrunc = Math.ceil(newLng);}
|
---|
211 | else{newLngTrunc = Math.floor(newLng);}
|
---|
212 |
|
---|
213 | //Construct query
|
---|
214 | query += "(";
|
---|
215 | query += "CS:\"" + coordToAbsDirected(newLatTrunc, "lat");
|
---|
216 | //query += "CS:\"" + coordToAbsDirected(newLatTrunc, "lat") + "\" \"" + coordToAbsDirected(newLngTrunc, "lng") + "\""; //query += "LA:" + coordToAbsDirected(newLatTrunc, "lat");
|
---|
217 | if(latDelta != 1)
|
---|
218 | {
|
---|
219 | query += newLatString.substring(newLatString.indexOf(".") + 1, newLatString.indexOf(".") + latPrecision + 1);
|
---|
220 | }
|
---|
221 | query += " ";
|
---|
222 | //query += "+AND+";
|
---|
223 |
|
---|
224 | //query += "LN:" + coordToAbsDirected(newLngTrunc, "lng");
|
---|
225 | query += coordToAbsDirected(newLngTrunc, "lng");
|
---|
226 | if(lngDelta != 1)
|
---|
227 | {
|
---|
228 | query += newLngString.substring(newLngString.indexOf(".") + 1, newLngString.indexOf(".") + lngPrecision + 1);
|
---|
229 | }
|
---|
230 | query += "\"";
|
---|
231 | query += ")";
|
---|
232 |
|
---|
233 | //if(i != (Math.floor(latDistance / latDelta) + 1) || j != (Math.floor(lngDistance / lngDelta) + 1)){ query += "+OR+"; }
|
---|
234 | if(i != iMax || j != jMax){ query += "+OR+"; }
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | // This works, why not from the double loop above?
|
---|
239 | //query = "(CS:\"" + coordToAbsDirected(newLatTrunc, "lat") + " " + coordToAbsDirected(newLngTrunc, "lng") + "\")";
|
---|
240 | //alert("@@@@in map-scripts::performSearchForMarkers() - query: " + query);
|
---|
241 |
|
---|
242 | //var url = gs.xsltParams.library_name + "?a=q&s=RawQuery&rt=rd&c=" + gs.cgiParams.c + "&s1.rawquery=" + query + "&excerptid=jsonNodes";
|
---|
243 | var url = gs.xsltParams.library_name;
|
---|
244 | var data = "a=q&s=RawQuery&rt=rd&c=" + gs.cgiParams.c + "&s1.rawquery=" + query + "&excerptid=jsonNodes";
|
---|
245 | $.ajax({type:"POST", url:url, data:data})
|
---|
246 | .success(function(responseText)
|
---|
247 | {
|
---|
248 | //console.log("*** responseText (first 250) = " + responseText.substring(0,256));
|
---|
249 |
|
---|
250 | if(responseText.search("id=\"jsonNodes") != -1)
|
---|
251 | {
|
---|
252 | var startIndex = responseText.indexOf(">");
|
---|
253 | var endIndex = responseText.indexOf("</");
|
---|
254 |
|
---|
255 | //console.log("@@@@ performSearch, got response: " + responseText);
|
---|
256 |
|
---|
257 | var jsonNodes = eval(responseText.substring(startIndex+1, endIndex));
|
---|
258 | if(jsonNodes && jsonNodes.length > 0)
|
---|
259 | {
|
---|
260 | for(var i = 0; i < jsonNodes.length; i++)
|
---|
261 | {
|
---|
262 | var doc = jsonNodes[i];
|
---|
263 |
|
---|
264 | var found = false;
|
---|
265 | for(var j = 0; j < _docList.ids.length; j++){if(doc.nodeID == _docList.ids[j]){found = true; break;}}
|
---|
266 |
|
---|
267 | if(!found)
|
---|
268 | {
|
---|
269 | _docList[doc.nodeID] = doc;
|
---|
270 | _docList.ids.push(doc.nodeID);
|
---|
271 |
|
---|
272 | createMarkers(doc, false);
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 | }
|
---|
277 | else
|
---|
278 | {
|
---|
279 | console.log("No JSON information received");
|
---|
280 | }
|
---|
281 |
|
---|
282 | _searchRunning = false;
|
---|
283 | }).fail(function(responseText, textStatus, errorThrown) // fail() has replaced error(), http://api.jquery.com/jquery.ajax/
|
---|
284 | {
|
---|
285 | console.log("In map-scripts.performSearchForMarkers(): Got an error in ajax call");
|
---|
286 | _searchRunning = false;
|
---|
287 | });
|
---|
288 | }
|
---|
289 |
|
---|
290 | function coordToAbsDirected(coord, type)
|
---|
291 | {
|
---|
292 | var value = "" + coord;
|
---|
293 | if(coord < 0)
|
---|
294 | {
|
---|
295 | value = value.substring(1);
|
---|
296 | if(type == "lat")
|
---|
297 | {
|
---|
298 | value += "S";
|
---|
299 | }
|
---|
300 | else
|
---|
301 | {
|
---|
302 | value += "W";
|
---|
303 | }
|
---|
304 | }
|
---|
305 | else
|
---|
306 | {
|
---|
307 | if(type == "lat")
|
---|
308 | {
|
---|
309 | value += "N";
|
---|
310 | }
|
---|
311 | else
|
---|
312 | {
|
---|
313 | value += "E";
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | return value;
|
---|
318 | }
|
---|
319 |
|
---|
320 | function updateMap()
|
---|
321 | {
|
---|
322 | var markersOnMap = 0;
|
---|
323 | var bounds = new google.maps.LatLngBounds();
|
---|
324 | for(var i = 0; i < _docList.ids.length; i++)
|
---|
325 | {
|
---|
326 | var doc = _docList.getDocByIndex(i);
|
---|
327 | if(doc.parentCL && doc.parentCL.style.display == "none")
|
---|
328 | {
|
---|
329 | if(doc.shapes) {
|
---|
330 | for(var x = 0; x < doc.shapes.length; x++) {
|
---|
331 | doc.shapes[x].setVisible(false);
|
---|
332 | }
|
---|
333 | markersOnMap = doc.shapes.length;
|
---|
334 | } else {
|
---|
335 | doc.marker.setVisible(false);
|
---|
336 | }
|
---|
337 | continue;
|
---|
338 | }
|
---|
339 | else
|
---|
340 | {
|
---|
341 | if(doc.shapes) {
|
---|
342 | for(var x = 0; x < doc.shapes.length; x++) {
|
---|
343 | doc.shapes[x].setVisible(true);
|
---|
344 | }
|
---|
345 | markersOnMap = doc.shapes.length;
|
---|
346 | } else {
|
---|
347 | doc.marker.setVisible(true);
|
---|
348 | }
|
---|
349 | markersOnMap++;
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | if(doc.coords) {
|
---|
354 | for(var x = 0; x < doc.coords.length; x++) {
|
---|
355 | var coord = doc.coords[x];
|
---|
356 | var coordInfo = getLatLngForCoord(doc.coords[x]);
|
---|
357 | bounds.extend(new google.maps.LatLng(coordInfo.lat, coordInfo.lng));
|
---|
358 | }
|
---|
359 | }
|
---|
360 | else {
|
---|
361 | bounds.extend(new google.maps.LatLng(doc.lat, doc.lng));
|
---|
362 | }
|
---|
363 | }
|
---|
364 |
|
---|
365 | if(markersOnMap > 1)
|
---|
366 | {
|
---|
367 | _map.fitBounds(bounds);
|
---|
368 | } else if (markersOnMap == 1) {
|
---|
369 | // sometimes a single point bounds are too small for the map to display, so use center and zoom instead of fitbounds.
|
---|
370 | _map.setCenter(bounds.getCenter());
|
---|
371 | _map.setZoom(18); // arbitrary value that looked nice for my example
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | // TODO: FUNCTION DUPLICATED IN panoramaViewer.js
|
---|
376 | function getLatLngForCoord(coord) {
|
---|
377 |
|
---|
378 | // https://stackoverflow.com/questions/2559318/how-to-check-for-an-undefined-or-null-variable-in-javascript
|
---|
379 | if(!coord) {
|
---|
380 | // some_variable is either null, undefined, 0, NaN, false, or an empty string
|
---|
381 | console.log("@@@@ In map-scripts::getLatLngForCoord(): no or invalid coord info");
|
---|
382 | return null;
|
---|
383 | }
|
---|
384 |
|
---|
385 | // coord is of the form: "37S77 157E53"
|
---|
386 | // lat will be 37S77, lng 157E53.
|
---|
387 | var indexOfSpace = coord.indexOf(" ");
|
---|
388 | if(indexOfSpace === -1) {
|
---|
389 | console.log("@@@@ In map-scripts::getLatLngForCoord(): bad format for coord " + coord);
|
---|
390 | return null;
|
---|
391 | }
|
---|
392 | var latitude = coord.substring(0, indexOfSpace);
|
---|
393 | var longitude = coord.substring(indexOfSpace+1);
|
---|
394 | return {lat: latitude, lng: longitude}; // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
|
---|
395 | }
|
---|
396 |
|
---|
397 | function loopThroughMarkers()
|
---|
398 | {
|
---|
399 | if(_docList.ids.length == 0)
|
---|
400 | {
|
---|
401 | return;
|
---|
402 | }
|
---|
403 |
|
---|
404 | var visibleMarkers = new Array();
|
---|
405 | for(var i = 0; i < _docList.ids.length; i++)
|
---|
406 | {
|
---|
407 | var doc = _docList.getDocByIndex(i);
|
---|
408 | if(doc.marker.getVisible())
|
---|
409 | {
|
---|
410 | visibleMarkers.push(doc);
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | if(visibleMarkers.length < 2)
|
---|
415 | {
|
---|
416 | clearAllInfoBoxes();
|
---|
417 | return;
|
---|
418 | }
|
---|
419 |
|
---|
420 | clearAllInfoBoxes();
|
---|
421 |
|
---|
422 | var elem = null;
|
---|
423 | while(!elem)
|
---|
424 | {
|
---|
425 | if(_docList.loopIndex >= visibleMarkers.length)
|
---|
426 | {
|
---|
427 | _docList.loopIndex = 0;
|
---|
428 | }
|
---|
429 |
|
---|
430 | var doc = visibleMarkers[_docList.loopIndex];
|
---|
431 | var elem = gs.jqGet("div" + doc.nodeID);
|
---|
432 | if(elem.length)
|
---|
433 | {
|
---|
434 | elem.css("background", "#BBFFBB");
|
---|
435 | setTimeout(function(){elem.css("background", "");}, 2000);
|
---|
436 | }
|
---|
437 | _docList.loopIndex++;
|
---|
438 | }
|
---|
439 | doc.marker.markerInfo.open(_map, doc.marker);
|
---|
440 | }
|
---|
441 |
|
---|
442 |
|
---|
443 | function focusDocument(id)
|
---|
444 | {
|
---|
445 | var doc = _docList[id];
|
---|
446 | if(doc)
|
---|
447 | {
|
---|
448 | clearInterval(_intervalHandle);
|
---|
449 | _intervalHandle = null;
|
---|
450 | if(doc.coords) {
|
---|
451 | for(var x = 0; x < doc.coords.length; x++) {
|
---|
452 | var coord = doc.coords[x];
|
---|
453 | var coordInfo = getLatLngForCoord(doc.coords[x]);
|
---|
454 | _map.panTo(new google.maps.LatLng(coordInfo.lat, coordInfo.lng));
|
---|
455 | }
|
---|
456 | } else {
|
---|
457 | _map.panTo(new google.maps.LatLng(doc.lat, doc.lng));
|
---|
458 | }
|
---|
459 | clearAllInfoBoxes();
|
---|
460 | doc.marker.markerInfo.open(_map, doc.marker);
|
---|
461 | var scrollCheckbox = $("#scrollCheckbox");
|
---|
462 | if(scrollCheckbox.checked)
|
---|
463 | {
|
---|
464 | scrollCheckbox.checked = false;
|
---|
465 | }
|
---|
466 | }
|
---|
467 | }
|
---|
468 |
|
---|
469 | function clearAllInfoBoxes()
|
---|
470 | {
|
---|
471 | for(var i = 0; i < _docList.ids.length; i++)
|
---|
472 | {
|
---|
473 | var doc = _docList.getDocByIndex(i);
|
---|
474 | doc.marker.markerInfo.close();
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | function createMarkers(doc, mainMarker) {
|
---|
479 | if(doc.mapoverlay) {
|
---|
480 | //console.log("Have shapes: " + doc.mapoverlay.toString());
|
---|
481 | createShapes(doc, mainMarker);
|
---|
482 | } else { // backwards compatible to deal with Lat and Lng meta stored for doc
|
---|
483 | pos = new google.maps.LatLng(doc.lat,doc.lng);
|
---|
484 | createMarker(doc, pos, mainMarker);
|
---|
485 | }
|
---|
486 | }
|
---|
487 |
|
---|
488 | function addInfoMarker(doc, shape) {
|
---|
489 |
|
---|
490 | if(!shape.description) {
|
---|
491 | //console.log("@@@@ " + shape.type.toString() + " had no description/label");
|
---|
492 | return;
|
---|
493 | }
|
---|
494 |
|
---|
495 | // else add an InfoWindow for this shape using the label (shape.description)
|
---|
496 |
|
---|
497 | // https://developers.google.com/maps/documentation/javascript/infowindows
|
---|
498 | // An InfoWindow's "position contains the LatLng at which this info window is anchored.
|
---|
499 | // Note: An InfoWindow may be attached either to a Marker object (in which case its position is based on the marker's location)
|
---|
500 | // or on the map itself at a specified LatLng. Opening an info window on a marker will automatically update the position."
|
---|
501 | var infoWindow = new google.maps.InfoWindow({content:shape.description}); // NOTE: if not setting content or position properties
|
---|
502 | // inside this constructor, need to call setContent/setPosition to set them
|
---|
503 |
|
---|
504 | if(shape.type === google.maps.drawing.OverlayType.MARKER) {
|
---|
505 | var marker = shape;
|
---|
506 | marker.addListener('mouseover', function() {
|
---|
507 | infoWindow.open(_map, marker);
|
---|
508 | });
|
---|
509 | marker.addListener('mouseout', function() {
|
---|
510 | infoWindow.close();
|
---|
511 | });
|
---|
512 | attachClickHandler(marker, doc.nodeID); // do what the original code used to do here
|
---|
513 | }
|
---|
514 | else {
|
---|
515 | var coord = ShapesUtil.getLabelCoordinate(shape);
|
---|
516 | console.log("Coord for " + shape.type.toString() + " is " + coord.toString());
|
---|
517 | infoWindow.setPosition(coord);
|
---|
518 | shape.addListener('mouseover', function() {
|
---|
519 | infoWindow.open(_map);
|
---|
520 | });
|
---|
521 | shape.addListener('mouseout', function() {
|
---|
522 | infoWindow.close();
|
---|
523 | });
|
---|
524 | attachClickHandler(shape, doc.nodeID); // as above
|
---|
525 | }
|
---|
526 | }
|
---|
527 |
|
---|
528 | // This function will create Google Shapes/Overlays and markers out of a given doc JSONNode's doc.mapOverlay
|
---|
529 | // (doc.mapOverlay shapes are stored as an array of JSON) and store the shapes/overlays in the doc.shapes array.
|
---|
530 | // Note: doc.coords are just an array of all the coordinates in a doc, not indicating to which shapes they belong.
|
---|
531 | // They're only **for searching** - for seaching which coords (hence docs) are in a map.
|
---|
532 | // Param mainMarker: if set to true, marker is red. If false, marker is blue
|
---|
533 | function createShapes(doc, mainMarker)
|
---|
534 | {
|
---|
535 | // for doc.shapes: don't store JSON anymore, convert them to google Shapes overlays and store them instead
|
---|
536 | doc.shapes = [];
|
---|
537 | for (var i=0; i<doc.mapoverlay.length; i++) {
|
---|
538 | var shape = ShapesUtil.JSONToShape(doc.mapoverlay[i]);
|
---|
539 | doc.shapes[i] = shape;
|
---|
540 | shape.setMap(_map);
|
---|
541 | shape["title"] = doc.title;
|
---|
542 |
|
---|
543 | // Unset editable and draggable properties of shape
|
---|
544 | // And for markers, which are initialised to clickable besides, undo the clickability
|
---|
545 | // and set them
|
---|
546 | if(shape.type === google.maps.drawing.OverlayType.MARKER) {
|
---|
547 | var marker = shape;
|
---|
548 | // mainMarkers should be red
|
---|
549 | if(!mainMarker) {
|
---|
550 | marker["icon"] = "interfaces/" + gs.xsltParams.interface_name + "/images/bluemarker.png";
|
---|
551 | }
|
---|
552 | marker.clickable = false; // only markers
|
---|
553 | }
|
---|
554 |
|
---|
555 | shape.editable = false;
|
---|
556 | shape.draggable = false;
|
---|
557 |
|
---|
558 |
|
---|
559 |
|
---|
560 | // doc[i]'s label = doc.shapes[i].description
|
---|
561 | addInfoMarker(doc, shape);
|
---|
562 | }
|
---|
563 |
|
---|
564 | var docElement = gs.jqGet("div" + doc.nodeID);
|
---|
565 | var parent;
|
---|
566 | if(docElement)
|
---|
567 | {
|
---|
568 | parent = docElement.parentNode;
|
---|
569 | }
|
---|
570 |
|
---|
571 | while(parent && parent.nodeName != "BODY")
|
---|
572 | {
|
---|
573 | if($(parent).attr("id") && $(parent).attr("id").search("divCL") != -1)
|
---|
574 | {
|
---|
575 | doc.parentCL = parent;
|
---|
576 | break;
|
---|
577 | }
|
---|
578 |
|
---|
579 | parent = parent.parentNode;
|
---|
580 | }
|
---|
581 | }
|
---|
582 |
|
---|
583 | // This method is only for backwards compatibility: for those collections with docs that only have Lat and Lng meta
|
---|
584 | // and no GPS.mapOverlay (and hence Coordinate) meta.
|
---|
585 | // Param mainMarker: if set to true, marker is red. If false, marker is blue
|
---|
586 | function createMarker(doc, pos, mainMarker)
|
---|
587 | {
|
---|
588 | var marker;
|
---|
589 | if(mainMarker)
|
---|
590 | {
|
---|
591 | marker = new google.maps.Marker
|
---|
592 | ({
|
---|
593 | position: pos,
|
---|
594 | title:doc.title,
|
---|
595 | map:_map
|
---|
596 | });
|
---|
597 | }
|
---|
598 | else
|
---|
599 | {
|
---|
600 | marker = new google.maps.Marker
|
---|
601 | ({
|
---|
602 | position: pos,
|
---|
603 | title:doc.title,
|
---|
604 | map:_map,
|
---|
605 | icon:"interfaces/" + gs.xsltParams.interface_name + "/images/bluemarker.png"
|
---|
606 | });
|
---|
607 | }
|
---|
608 |
|
---|
609 | var docElement = gs.jqGet("div" + doc.nodeID);
|
---|
610 | var parent;
|
---|
611 | if(docElement)
|
---|
612 | {
|
---|
613 | parent = docElement.parentNode;
|
---|
614 | }
|
---|
615 |
|
---|
616 | while(parent && parent.nodeName != "BODY")
|
---|
617 | {
|
---|
618 | if($(parent).attr("id") && $(parent).attr("id").search("divCL") != -1)
|
---|
619 | {
|
---|
620 | doc.parentCL = parent;
|
---|
621 | break;
|
---|
622 | }
|
---|
623 |
|
---|
624 | parent = parent.parentNode;
|
---|
625 | }
|
---|
626 |
|
---|
627 | var info = new google.maps.InfoWindow({content:doc.title});
|
---|
628 | marker.markerInfo = info;
|
---|
629 | doc.marker = marker;
|
---|
630 | attachClickHandler(marker, doc.nodeID);
|
---|
631 | }
|
---|
632 |
|
---|
633 | // TODO: with the following, it seems that clicking on shape expands the entire document
|
---|
634 | // Should it be that clicking on a shape should expand the doc section that contains that shape meta?
|
---|
635 | function attachClickHandler(shapeOrMarker, nodeID)
|
---|
636 | {
|
---|
637 | google.maps.event.addListener(shapeOrMarker, 'click', function()
|
---|
638 | {
|
---|
639 | document.location.href = gs.xsltParams.library_name + "?a=d&ed=1&c=" + gs.cgiParams.c + "&d=" + nodeID + "&dt=hierarchy&p.a=b&p.sa=&p.s=ClassifierBrowse";
|
---|
640 | });
|
---|
641 | }
|
---|
642 |
|
---|
643 | function NewLatLng(lat, lng)
|
---|
644 | {
|
---|
645 | console.log("Latitude " + lat);
|
---|
646 | console.log("Longitude " + lng);
|
---|
647 | }
|
---|
648 |
|
---|
649 | function getSubClassifier(sectionID)
|
---|
650 | {
|
---|
651 | var url = gs.xsltParams.library_name + "?a=b&rt=s&s=ClassifierBrowse&c=" + gs.cgiParams.c + "&cl=" + sectionID + "&excerptid=jsonNodes";
|
---|
652 | $.ajax(url)
|
---|
653 | .success(function(responseText)
|
---|
654 | {
|
---|
655 | var startIndex = responseText.indexOf(">");
|
---|
656 | var endIndex = responseText.indexOf("</");
|
---|
657 |
|
---|
658 | var jsonNodes = eval(responseText.substring(startIndex+1, endIndex));
|
---|
659 | if(jsonNodes && jsonNodes.length > 0)
|
---|
660 | {
|
---|
661 | for(var i = 0; i < jsonNodes.length; i++)
|
---|
662 | {
|
---|
663 | var doc = jsonNodes[i];
|
---|
664 | _docList[doc.nodeID] = doc;
|
---|
665 | _docList.ids.push(doc.nodeID);
|
---|
666 |
|
---|
667 | createMarkers(doc, true);
|
---|
668 | }
|
---|
669 |
|
---|
670 | $("#map_canvas").css({"visibility": "visible", "height": ""});
|
---|
671 | }
|
---|
672 |
|
---|
673 | updateMap();
|
---|
674 | })
|
---|
675 | .error(function()
|
---|
676 | {
|
---|
677 | console.log("Error getting subclassifiers");
|
---|
678 | return;
|
---|
679 | });
|
---|
680 | }
|
---|
681 |
|
---|
682 | function performDistanceSearchWithCoordinates(id, coord, degrees)
|
---|
683 | {
|
---|
684 | var coordInfo = getLatLngForCoord(coord);
|
---|
685 | if(!coordInfo) {
|
---|
686 | console.log("@@@ ERROR in map-scripts::performDistanceSearchWithCoordinates: coordInfo is null");
|
---|
687 | }
|
---|
688 | performDistanceSearch(id, coordInfo.lat, coordInfo.lng, degrees);
|
---|
689 | }
|
---|
690 |
|
---|
691 | function performDistanceSearch(id, lat, lng, degrees)
|
---|
692 | {
|
---|
693 | if(parseFloat(lat) > 180 || parseFloat(lat) < -180 || parseFloat(lng) > 180 || parseFloat(lat) < -180)
|
---|
694 | {
|
---|
695 | console.log("Latitude or longitude incorrectly formatted");
|
---|
696 | return;
|
---|
697 | }
|
---|
698 |
|
---|
699 | if(lat.indexOf(".") == -1 || lng.indexOf(".") == -1 || (lat.indexOf(".") + 3) >= lat.length || (lng.indexOf(".") + 3) >= lng.length)
|
---|
700 | {
|
---|
701 | console.log("Latitude or longitude does not have the required precision for a distance search");
|
---|
702 | return;
|
---|
703 | }
|
---|
704 |
|
---|
705 | var query = "";
|
---|
706 | for(var i = 0; i < degrees * 2; i++)
|
---|
707 | {
|
---|
708 | for (var j = 0; j < degrees * 2; j++)
|
---|
709 | {
|
---|
710 | var latDelta = (i - degrees) * 0.01;
|
---|
711 | var lngDelta = (j - degrees) * 0.01;
|
---|
712 |
|
---|
713 | query += "(" + getDistanceQueryString(lat, latDelta, 2, "LA", ["N","S"]);
|
---|
714 | query += "+AND+";
|
---|
715 | query += getDistanceQueryString(lng, lngDelta, 2, "LN", ["E","W"]) + ")";
|
---|
716 |
|
---|
717 | if(i != ((degrees * 2) - 1) || j != ((degrees * 2) - 1)){ query += "+OR+"; }
|
---|
718 | }
|
---|
719 | }
|
---|
720 |
|
---|
721 | var inlineTemplate = '\
|
---|
722 | <xsl:template match="/" priority="5">\
|
---|
723 | <table id="nearbyDocs">\
|
---|
724 | <tr>\
|
---|
725 | <th><a href="javascript:sortByDistance();">Distance</a></th><th><a href="javascript:sortAlphabetically();">Document</a></th>\
|
---|
726 | </tr>\
|
---|
727 | <xsl:apply-templates select="//documentNode"/>\
|
---|
728 | </table>\
|
---|
729 | </xsl:template>\
|
---|
730 | \
|
---|
731 | <xsl:template match="documentNode" priority="5">\
|
---|
732 | <xsl:if test="@nodeID !=\''+id+'\'">\
|
---|
733 | <tr>\
|
---|
734 | <td>___<gsf:metadata name="Latitude"/>______<gsf:metadata name="Longitude"/>___</td>\
|
---|
735 | <td><gsf:link title="'+gs.text.doc.nearby_doc_tooltip+'" type="document"><gsf:metadata name="Title"/></gsf:link></td>\
|
---|
736 | </tr>\
|
---|
737 | </xsl:if>\
|
---|
738 | </xsl:template>';
|
---|
739 |
|
---|
740 | var url = gs.xsltParams.library_name + "?a=q&s=RawQuery&rt=rd&c=" + gs.cgiParams.c + "&s1.rawquery=" + query + "&excerptid=nearbyDocs&ilt=" + inlineTemplate.replace(/ /, "%20");
|
---|
741 | $.ajax(url)
|
---|
742 | .success(function(response)
|
---|
743 | {
|
---|
744 | response = response.replace(/<img src="[^"]*map_marker.png"[^>]*>/g, "");
|
---|
745 |
|
---|
746 | var nearbyDocsArray = new Array();
|
---|
747 |
|
---|
748 | var lats = new Array();
|
---|
749 | var lngs = new Array();
|
---|
750 | var matches = response.match(/___(-?[0-9\.]*)___/g);
|
---|
751 | for(var i = 0; i < matches.length; i += 2)
|
---|
752 | {
|
---|
753 | var matchLatFloat = parseFloat(matches[i].replace("___", ""));
|
---|
754 | var matchLngFloat = parseFloat(matches[i+1].replace("___", ""));
|
---|
755 |
|
---|
756 | lats.push(matchLatFloat);
|
---|
757 | lngs.push(matchLngFloat);
|
---|
758 | var distance = Math.sqrt(Math.pow(matchLatFloat - parseFloat(lat), 2) + Math.pow(matchLngFloat - parseFloat(lng), 2)) * (40000.0/360.0);
|
---|
759 | var distanceString = "" + distance;
|
---|
760 | distanceString = distanceString.substring(0, 6);
|
---|
761 | response = response.replace(matches[i] + matches[i+1], distanceString);
|
---|
762 | }
|
---|
763 |
|
---|
764 | var index = 0;
|
---|
765 | var i = 0;
|
---|
766 | while(true)
|
---|
767 | {
|
---|
768 | var distanceStart = response.indexOf("<td>", index);
|
---|
769 | if(distanceStart == -1)
|
---|
770 | {
|
---|
771 | break;
|
---|
772 | }
|
---|
773 | var distanceEnd = response.indexOf("</td>", distanceStart);
|
---|
774 |
|
---|
775 | var docLinkStart = response.indexOf("<td>", distanceEnd);
|
---|
776 | var docLinkEnd = response.indexOf("</td>", docLinkStart);
|
---|
777 |
|
---|
778 | var dist = response.substring(distanceStart + 4, distanceEnd);
|
---|
779 | var docLink = response.substring(docLinkStart + 4, docLinkEnd);
|
---|
780 |
|
---|
781 | _nearbyDocs.push({title:docLink, distance:dist, lat:lats[i], lng:lngs[i++]});
|
---|
782 |
|
---|
783 | index = docLinkEnd;
|
---|
784 | }
|
---|
785 |
|
---|
786 | sortByDistance(lat,lng);
|
---|
787 |
|
---|
788 | var toggle = $("#nearbyDocumentsToggle");
|
---|
789 | toggle.attr("src", gs.imageURLs.collapse);
|
---|
790 | gs.functions.makeToggle(toggle, $("#nearbyDocuments"));
|
---|
791 | });
|
---|
792 | }
|
---|
793 |
|
---|
794 | var map_centering_timeout = null;
|
---|
795 | function recenterMapF(lat, lng)
|
---|
796 | {
|
---|
797 | return function() {
|
---|
798 | _map.setCenter(new google.maps.LatLng(lat, lng));
|
---|
799 | }
|
---|
800 | }
|
---|
801 | function recenterMap(lat, lng)
|
---|
802 | {
|
---|
803 |
|
---|
804 | _map.setCenter(new google.maps.LatLng(lat, lng));
|
---|
805 |
|
---|
806 | }
|
---|
807 | function sortByDistance(base_lat,base_lng)
|
---|
808 | {
|
---|
809 | var sortedTable = '<table id="nearbyDocs" onmouseleave="clearTimeout(map_centering_timeout); recenterMap('+base_lat+','+base_lng+');"><tr><th><a href="javascript:;">Distance</a></th><th><a href="javascript:sortAlphabetically('+base_lat+', '+base_lng+');">Document</a></th></tr>';
|
---|
810 | _nearbyDocs.sort(function(a, b){return (a.distance - b.distance);});
|
---|
811 | for(var i = 0; i < _nearbyDocs.length; i++)
|
---|
812 | {
|
---|
813 |
|
---|
814 | sortedTable += "<tr><td>" + prettifyDistance(_nearbyDocs[i].distance) + '</td><td onmouseover="clearTimeout(map_centering_timeout); map_centering_timeout = setTimeout(recenterMapF(' + _nearbyDocs[i].lat + ',' + _nearbyDocs[i].lng + '), 900)" >' + _nearbyDocs[i].title + "</td></tr>";
|
---|
815 | }
|
---|
816 | sortedTable += "</table>";
|
---|
817 |
|
---|
818 | $("#nearbyDocuments").html(sortedTable);
|
---|
819 | }
|
---|
820 | function prettifyDistance(distance) {
|
---|
821 |
|
---|
822 | var new_distance;
|
---|
823 | if (distance < 1) {
|
---|
824 | new_distance = (distance * 1000);
|
---|
825 | // Round to nearest whole number - don't need to show points of metres..
|
---|
826 | new_distance = Math.round(new_distance);
|
---|
827 | new_distance += " m";
|
---|
828 | }
|
---|
829 | else {
|
---|
830 | new_distance = distance +" km";
|
---|
831 |
|
---|
832 | }
|
---|
833 | return new_distance;
|
---|
834 | }
|
---|
835 |
|
---|
836 |
|
---|
837 |
|
---|
838 | function sortAlphabetically(base_lat, base_lng)
|
---|
839 | {
|
---|
840 | var sortedTable = '<table id="nearbyDocs" onmouseleave="clearTimeout(map_centering_timeout); recenterMap('+base_lat+','+base_lng+');"><tr><th><a href="javascript:sortByDistance('+base_lat+', '+base_lng+');">Distance</a></th><th><a href="javascript:;">Document</a></th></tr>';
|
---|
841 | _nearbyDocs.sort(function(a, b)
|
---|
842 | {
|
---|
843 | var firstTitleStartIndex = a.title.indexOf(">");
|
---|
844 | var firstTitleEndIndex = a.title.indexOf("<", firstTitleStartIndex);
|
---|
845 | var firstTitle = a.title.substring(firstTitleStartIndex + 1, firstTitleEndIndex);
|
---|
846 | var secondTitleStartIndex = b.title.indexOf(">");
|
---|
847 | var secondTitleEndIndex = b.title.indexOf("<", secondTitleStartIndex);
|
---|
848 | var secondTitle = b.title.substring(secondTitleStartIndex + 1, secondTitleEndIndex);
|
---|
849 | return ((firstTitle.toLowerCase() == secondTitle.toLowerCase()) ? 0 : ((firstTitle.toLowerCase() > secondTitle.toLowerCase()) ? 1 : -1));
|
---|
850 | });
|
---|
851 | for(var i = 0; i < _nearbyDocs.length; i++)
|
---|
852 | {
|
---|
853 | sortedTable += "<tr><td>" + _nearbyDocs[i].distance + '</td><td onmouseover="clearTimeout(map_centering_timeout); map_centering_timeout = setTimeout(recenterMapF(' + _nearbyDocs[i].lat + ',' + _nearbyDocs[i].lng + '), 900)">' + _nearbyDocs[i].title + "</td></tr>";
|
---|
854 | }
|
---|
855 | sortedTable += "</table>";
|
---|
856 |
|
---|
857 | $("#nearbyDocuments").html(sortedTable);
|
---|
858 | }
|
---|
859 |
|
---|
860 | function getDistanceQueryString(currentCoord, delta, precision, indexName, dirs)
|
---|
861 | {
|
---|
862 | var query = "";
|
---|
863 | var coordFloat = parseFloat(currentCoord);
|
---|
864 |
|
---|
865 | var newCoord = "" + (coordFloat + delta);
|
---|
866 | var beforeDec = newCoord.substring(0, newCoord.indexOf("."));
|
---|
867 |
|
---|
868 | var dir = dirs[0];
|
---|
869 | if(coordFloat < 0)
|
---|
870 | {
|
---|
871 | dir = dirs[1];
|
---|
872 | beforeDec = beforeDec.substring(1);
|
---|
873 | }
|
---|
874 | beforeDec += dir;
|
---|
875 |
|
---|
876 | var afterDec = newCoord.substring(newCoord.indexOf(".") + 1, newCoord.indexOf(".") + (precision) + 1);
|
---|
877 |
|
---|
878 | return indexName + ":" + beforeDec + "+AND+" + indexName + ":" + afterDec;
|
---|
879 | }
|
---|