lib.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. Copyright (c) 2013 Mapo developpers and contributors <mapo.tizen@gmail.com>
  3. This file is part of Mapo.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. var map;
  16. var isOnline = false;
  17. var recording = false;
  18. var recordingFrequency;
  19. var energySaving = false;
  20. /*
  21. * WGS Manager
  22. */
  23. function toStringWgs84(lat, lon) {
  24. var latitude = lat;
  25. var longitude = lon;
  26. var text = "";
  27. {
  28. if (longitude > 0) {
  29. text += "E";
  30. } else {
  31. text += "W";
  32. longitude = -longitude;
  33. }
  34. var u = longitude;
  35. var d = parseInt("" + u, 10);
  36. var s = (u - d) * 60 * 60;
  37. var m = (s / 60.);
  38. m = parseInt("" + m, 10);
  39. s = (s - 60 * m);
  40. s = parseInt("" + s, 10);
  41. text += "" + d + "d" + m + "m" + s + "s";
  42. }
  43. // text += "\n";
  44. {
  45. if (latitude > 0) {
  46. text += "N";
  47. } else {
  48. latitude = -latitude;
  49. text += "S";
  50. }
  51. var u = latitude;
  52. var d = parseInt("" + u, 10);
  53. var s = (u - d) * 60 * 60;
  54. var m = (s / 60.);
  55. m = parseInt("" + m, 10);
  56. s = (s - 60 * m);
  57. s = parseInt("" + s, 10);
  58. text += "" + d + "d" + m + "m" + s + "s";
  59. }
  60. return text;
  61. }
  62. function toStringText(lat, lon) {
  63. var latitude = lat;
  64. var longitude = lon;
  65. var text = "";
  66. {
  67. if (longitude > 0) {
  68. text += "E";
  69. } else {
  70. text += "W";
  71. longitude = -longitude;
  72. }
  73. var u = longitude;
  74. var d = parseInt("" + u, 10);
  75. var s = (u - d) * 60 * 60;
  76. var m = (s / 60.);
  77. m = parseInt("" + m, 10);
  78. s = (s - 60 * m);
  79. s = parseInt("" + s, 10);
  80. text = d + " ° " + text + " D " + m + " m " + s + " s ";
  81. }
  82. // text += "\n";
  83. {
  84. if (latitude > 0) {
  85. text += "N";
  86. } else {
  87. latitude = -latitude;
  88. text += "S";
  89. }
  90. var u = latitude;
  91. var d = parseInt("" + u, 10);
  92. var s = (u - d) * 60 * 60;
  93. var m = (s / 60.);
  94. m = parseInt("" + m, 10);
  95. s = (s - 60 * m);
  96. s = parseInt("" + s, 10);
  97. text = d + " ° " + text + " D " + m + " m " + s + " s ";
  98. }
  99. return text;
  100. }
  101. function setWGS(lat, lon) {
  102. var text = "wgs84:" + toStringWgs84(lat, lon);
  103. var text = toStringText(lat, lon);
  104. document.getElementById("wgs").value = text;
  105. }
  106. /*
  107. * Link Manager
  108. */
  109. function getOSMLink(lat, lon) {
  110. var url = "http://www.openstreetmap.org/?&zoom=10&layers=mapnik&lat=${lat}&lon=${lon}";
  111. url = url.replace("${lon}", lon);
  112. url = url.replace("${lat}", lat);
  113. return url;
  114. }
  115. function getGMLink(lat, lon) {
  116. var url = "http://maps.google.com/maps?&z=10&ll=${lat},${lon}";
  117. url = url.replace("${lon}", lon);
  118. url = url.replace("${lat}", lat);
  119. return url;
  120. }
  121. function updateLinks(lat, lon) {
  122. $('#OSMLink').val(getOSMLink(lat, lon));
  123. $('#GMLink').val(getGMLink(lat, lon));
  124. }
  125. /*
  126. * Map Manager
  127. */
  128. function getMapSize() {
  129. var viewHeight = $(window).height();
  130. var viewWidth = $(window).width();
  131. var header = $("div[data-role='header']:visible:visible");
  132. var footer = $("div[data-role='footer']:visible:visible");
  133. var navbar = $("div[data-role='navbar']:visible:visible");
  134. var content = $("div[data-role='content']:visible:visible");
  135. var contentHeight = viewHeight-header.outerHeight()-navbar.outerHeight()-footer.outerHeight();
  136. var contentWidth = viewWidth-30;
  137. return [contentWidth, contentHeight];
  138. }
  139. function setMapSize() {
  140. var mapSize = getMapSize();
  141. $('#myMap').css("width", mapSize[0]);
  142. $('#myMap').css("height", mapSize[1]);
  143. }
  144. function chargeMap(lat, lon) {
  145. map = new OpenLayers.Map('myMap', {
  146. projection : 'EPSG:3857',
  147. layers : [ new OpenLayers.Layer.OSM("OpenStreetMap"),
  148. new OpenLayers.Layer.Google("Google Streets", {
  149. numZoomLevels : 20
  150. }), new OpenLayers.Layer.Google("Google Physical", {
  151. type : google.maps.MapTypeId.TERRAIN
  152. }), new OpenLayers.Layer.Google("Google Hybrid", {
  153. type : google.maps.MapTypeId.HYBRID,
  154. numZoomLevels : 20
  155. }), new OpenLayers.Layer.Google("Google Satellite", {
  156. type : google.maps.MapTypeId.SATELLITE,
  157. numZoomLevels : 22
  158. }) ],
  159. center : new OpenLayers.LonLat(lon, lat)
  160. .transform('EPSG:4326', 'EPSG:3857'),
  161. zoom : 7
  162. });
  163. map.addControl(new OpenLayers.Control.LayerSwitcher());
  164. }
  165. /*
  166. * Latitude/Lontitude Manager
  167. */
  168. function setLat(lat) {
  169. if (document.getElementById("lat").value != lat) {
  170. document.getElementById("lat").value = lat;
  171. }
  172. }
  173. function setLon(lon) {
  174. if (document.getElementById("lon").value != lon) {
  175. document.getElementById("lon").value = lon;
  176. }
  177. }
  178. /*
  179. * General Manager
  180. */
  181. function set(lat, lon) {
  182. setLat(lat);
  183. setLon(lon);
  184. setWGS(lat, lon);
  185. updateLinks(lat, lon);
  186. $('#myMap').empty();
  187. if (isOnline) {
  188. setMapSize();
  189. chargeMap(lat, lon);
  190. } else {
  191. $('#myMap').html(
  192. "<p align='center'>Please connect your application online in the settings"
  193. + " if you want to charge the map</p>");
  194. }
  195. }
  196. function refresh() {
  197. if (isOnline && !navigator.onLine) {
  198. $('#switchOnline').val('offline').slider('refresh');
  199. isOnline = false;
  200. alert("Connection lost");
  201. }
  202. set(document.getElementById("lat").value,
  203. document.getElementById("lon").value);
  204. }
  205. /*
  206. * Location Manager
  207. */
  208. function showLocation(position) {
  209. set(position.coords.latitude, position.coords.longitude);
  210. }
  211. function errorLocation(error) {
  212. var errorInfo = document.getElementById("location");
  213. switch (error.code) {
  214. case error.PERMISSION_DENIED:
  215. errorInfo.innerHTML = "User denied the request for Geolocation.";
  216. break;
  217. case error.POSITION_UNAVAILABLE:
  218. errorInfo.innerHTML = "Location information is unavailable.";
  219. break;
  220. case error.TIMEOUT:
  221. errorInfo.innerHTML = "The request to get user location timed out.";
  222. break;
  223. case error.UNKNOWN_ERROR:
  224. errorInfo.innerHTML = "An unknown error occurred.";
  225. break;
  226. }
  227. }
  228. function getLocation() {
  229. if (navigator.geolocation) {
  230. navigator.geolocation.getCurrentPosition(showLocation, errorLocation, {
  231. enableHighAccuracy : !energySaving,
  232. });
  233. } else {
  234. document.getElementById("location").innerHTML =
  235. "Geolocation is not supported by this browser.";
  236. }
  237. }
  238. /*
  239. * Recording manager
  240. */
  241. var docDir;
  242. var doc;
  243. var file;
  244. function onRecordError(e) {
  245. var msg = '';
  246. switch (e.code) {
  247. case FileError.QUOTA_EXCEEDED_ERR:
  248. msg = 'QUOTA_EXCEEDED_ERR';
  249. break;
  250. case FileError.NOT_FOUND_ERR:
  251. msg = 'NOT_FOUND_ERR';
  252. break;
  253. case FileError.SECURITY_ERR:
  254. msg = 'SECURITY_ERR';
  255. break;
  256. case FileError.INVALID_MODIFICATION_ERR:
  257. msg = 'INVALID_MODIFICATION_ERR';
  258. break;
  259. case FileError.INVALID_STATE_ERR:
  260. msg = 'INVALID_STATE_ERR';
  261. break;
  262. default:
  263. msg = 'Unknown Error';
  264. break;
  265. };
  266. console.log('Error: ' + msg);
  267. }
  268. function resolveFile(){
  269. try {
  270. file = docDir.resolve(doc);
  271. } catch (exc) {
  272. console.log('Could not resolve file: ' + exc.message);
  273. // Stop in case of any errors
  274. return;
  275. }
  276. }
  277. function writeToStream(fileStream) {
  278. try {
  279. var dateRecord = new Date();
  280. var lat = $("#lat").val();
  281. var lon = $("#lon").val();
  282. fileStream.write(dateRecord+" lat: "+lat+" lon: "+lon+"\r");
  283. fileStream.close();
  284. } catch (exc) {
  285. console.log('Could not write to file: ' + exc.message);
  286. }
  287. }
  288. function writeRecord(){
  289. try {
  290. file.openStream(
  291. // open for appending
  292. 'a',
  293. // success callback - add textarea's contents
  294. writeToStream,
  295. // error callback
  296. onRecordError
  297. );
  298. } catch (exc) {
  299. console.log('Could not write to file: ' + exc.message);
  300. }
  301. }
  302. function readFromStream(fileStream) {
  303. try {
  304. console.log('File size: ' + file.fileSize);
  305. var contents = fileStream.read(fileStream.bytesAvailable);
  306. fileStream.close();
  307. console.log('file contents:' + contents);
  308. } catch (exc) {
  309. console.log('Could not read from file: ' + exc.message);
  310. }
  311. }
  312. function readRecord(){
  313. try {
  314. file.openStream(
  315. // open for reading
  316. 'r',
  317. // success callback - add textarea's contents
  318. readFromStream,
  319. // error callback
  320. onRecordError
  321. );
  322. } catch (exc) {
  323. console.log('Could not write to file: ' + exc.message);
  324. }
  325. }
  326. function onResolveSuccess(dir) {
  327. docDir = dir;
  328. var dateFile = new Date();
  329. doc = 'MAPO_course_'+dateFile+".txt";
  330. doc = doc.replace(/ /g, "-");
  331. docDir.createFile(doc);
  332. alert("Course recording in the file : "+doc);
  333. }
  334. function onResolveError(e) {
  335. console.log('message: ' + e.message);
  336. }
  337. function recordLocation(position){
  338. if(recording){
  339. set(position.coords.latitude, position.coords.longitude);
  340. resolveFile();
  341. writeRecord();
  342. readRecord();
  343. }
  344. }
  345. function getPosition() {
  346. navigator.geolocation.getCurrentPosition(recordLocation, errorLocation,
  347. {enableHighAccuracy : !energySaving});
  348. }
  349. function record() {
  350. if (navigator.geolocation) {
  351. if(!recording){
  352. recording = true;
  353. setFrequency();
  354. tizen.filesystem.resolve('documents', onResolveSuccess, onResolveError, 'rw');
  355. var intervalID = setInterval(getPosition, recordingFrequency);
  356. }
  357. else {
  358. recording = false;
  359. clearInterval(intervalID);
  360. alert("Course recorded in the file : "+doc);
  361. }
  362. } else {
  363. document.getElementById("location").innerHTML = "Geolocation is not supported.";
  364. }
  365. }
  366. /*
  367. * Settings Manager
  368. */
  369. function switchOnline() {
  370. if (!isOnline) {
  371. if (navigator.onLine) {
  372. isOnline = true;
  373. } else {
  374. $('#switchOnline').val('offline').slider('refresh');
  375. alert("Can not connect");
  376. }
  377. } else {
  378. isOnline = false;
  379. }
  380. refresh();
  381. }
  382. function switchEnergy() {
  383. if(!energySaving){
  384. energySaving = true;
  385. }
  386. else {
  387. energySaving = false;
  388. }
  389. }
  390. function setFrequency(){
  391. recordingFrequency = $('#sliderFrequency').val()*1000; // From seconds to millisecond
  392. }
  393. function exit() {
  394. tizen.application.getCurrentApplication().exit();
  395. }