Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. var HOST = "http://**********"; // ask me for this in class
  2.  
  3. var URLS = {
  4. login: "/rest/tokenlogin/",
  5. userme: "/rest/userme/",
  6. updateposition: "/rest/updateposition/"
  7. };
  8.  
  9. var map;
  10.  
  11. var curIcon = L.ExtraMarkers.icon({
  12. icon: 'fa-crosshairs',
  13. iconColor: 'white',
  14. markerColor: 'blue',
  15. shape: 'square',
  16. prefix: 'fa'
  17. });
  18.  
  19. function onLoad() {
  20. console.log("In onLoad.");
  21. document.addEventListener('deviceready', onDeviceReady, false);
  22. }
  23.  
  24. function onDeviceReady() {
  25. console.log("In onDeviceReady.");
  26.  
  27. $("#btn-login").on("touchstart", loginPressed);
  28. $("#sp-logout").on("touchstart", logoutPressed);
  29.  
  30. if (localStorage.lastUserName && localStorage.lastUserPwd) {
  31. $("#in-username").val(localStorage.lastUserName);
  32. $("#in-password").val(localStorage.lastUserPwd);
  33. }
  34.  
  35. $(document).on("pagecreate", "#map-page", function (event) {
  36. console.log("In pagecreate. Target is " + event.target.id + ".");
  37.  
  38. $("#goto-currentlocation").on("touchstart", function () {
  39. getCurrentlocation();
  40. });
  41.  
  42. $("#map-page").enhanceWithin();
  43.  
  44. makeBasicMap();
  45. getCurrentlocation();
  46. });
  47.  
  48. $(document).on("pageshow", function (event) {
  49. console.log("In pageshow. Target is " + event.target.id + ".");
  50. if (!localStorage.authtoken) {
  51. $.mobile.navigate("#login-page");
  52. }
  53. setUserName();
  54. });
  55.  
  56. $(document).on("pageshow", "#map-page", function () {
  57. console.log("In pageshow / #map-page.");
  58. map.invalidateSize();
  59. });
  60.  
  61. $('div[data-role="page"]').page();
  62.  
  63. console.log("TOKEN: " + localStorage.authtoken);
  64. if (localStorage.authtoken) {
  65. $.mobile.navigate("#map-page");
  66. } else {
  67. $.mobile.navigate("#login-page");
  68. }
  69. }
  70.  
  71. function loginPressed() {
  72. console.log("In loginPressed.");
  73. $.ajax({
  74. type: "GET",
  75. url: HOST + URLS["login"],
  76. data: {
  77. username: $("#in-username").val(),
  78. password: $("#in-password").val()
  79. }
  80. }).done(function (data, status, xhr) {
  81. localStorage.authtoken = localStorage.authtoken = "Token " + xhr.responseJSON.token;
  82. localStorage.lastUserName = $("#in-username").val();
  83. localStorage.lastUserPwd = $("#in-password").val();
  84.  
  85. $.mobile.navigate("#map-page");
  86. }).fail(function (xhr, status, error) {
  87. var message = "Login Failed\n";
  88. if ((!xhr.status) && (!navigator.onLine)) {
  89. message += "Bad Internet Connection\n";
  90. }
  91. message += "Status: " + xhr.status + " " + xhr.responseText;
  92. showOkAlert(message);
  93. logoutPressed();
  94. });
  95. }
  96.  
  97. function logoutPressed() {
  98. console.log("In logoutPressed.");
  99. localStorage.removeItem("authtoken");
  100. $.mobile.navigate("#login-page");
  101. // $.ajax({
  102. // type: "GET",
  103. // headers: {"Authorization": localStorage.authtoken}
  104. // // url: HOST + URLS["logout"]
  105. // }).always(function () {
  106. // localStorage.removeItem("authtoken");
  107. // $.mobile.navigate("#login-page");
  108. // });
  109. }
  110.  
  111. function showOkAlert(message) {
  112. navigator.notification.alert(message, null, "WMAP 2017", "OK");
  113. }
  114.  
  115. function getCurrentlocation() {
  116. console.log("In getCurrentlocation.");
  117. var myLatLon;
  118. var myPos;
  119.  
  120. navigator.geolocation.getCurrentPosition(
  121. function (pos) {
  122. // myLatLon = L.latLng(pos.coords.latitude, pos.coords.longitude);
  123. myPos = new myGeoPosition(pos);
  124. localStorage.lastKnownCurrentPosition = JSON.stringify(myPos);
  125.  
  126. setMapToCurrentLocation();
  127. updatePosition();
  128. },
  129. function (err) {
  130. },
  131. {
  132. enableHighAccuracy: true
  133. // maximumAge: 60000,
  134. // timeout: 5000
  135. }
  136. );
  137. }
  138.  
  139. function setMapToCurrentLocation() {
  140. console.log("In setMapToCurrentLocation.");
  141. if (localStorage.lastKnownCurrentPosition) {
  142. var myPos = JSON.parse(localStorage.lastKnownCurrentPosition);
  143. var myLatLon = L.latLng(myPos.coords.latitude, myPos.coords.longitude);
  144. L.marker(myLatLon, {icon: curIcon}).addTo(map);
  145. map.flyTo(myLatLon, 15);
  146. }
  147. }
  148.  
  149. function updatePosition() {
  150. console.log("In updatePosition.");
  151. if (localStorage.lastKnownCurrentPosition) {
  152. var myPos = JSON.parse(localStorage.lastKnownCurrentPosition);
  153. $.ajax({
  154. type: "PATCH",
  155. headers: {
  156. "Content-Type": "application/x-www-form-urlencoded",
  157. "Authorization": localStorage.authtoken
  158. },
  159. url: HOST + URLS["updateposition"],
  160. data: {
  161. lat: myPos.coords.latitude,
  162. lon: myPos.coords.longitude
  163. }
  164. }).done(function (data, status, xhr) {
  165. showOkAlert("Position Updated");
  166. }).fail(function (xhr, status, error) {
  167. var message = "Position Update Failed\n";
  168. if ((!xhr.status) && (!navigator.onLine)) {
  169. message += "Bad Internet Connection\n";
  170. }
  171. message += "Status: " + xhr.status + " " + xhr.responseText;
  172. showOkAlert(message);
  173. }).always(function () {
  174. $.mobile.navigate("#map-page");
  175. });
  176. }
  177. }
  178.  
  179. function makeBasicMap() {
  180. console.log("In makeBasicMap.");
  181. map = L.map("map-var", {
  182. zoomControl: false,
  183. attributionControl: false
  184. }).fitWorld();
  185. L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  186. useCache: true
  187. }).addTo(map);
  188.  
  189. $("#leaflet-copyright").html("Leaflet | Map Tiles &copy; <a href='http://openstreetmap.org'>OpenStreetMap</a> contributors");
  190. }
  191.  
  192. function myGeoPosition(p) {
  193. this.coords = {};
  194. this.coords.latitude = p.coords.latitude;
  195. this.coords.longitude = p.coords.longitude;
  196. this.coords.accuracy = (p.coords.accuracy) ? p.coords.accuracy : 0;
  197. this.timestamp = (p.timestamp) ? p.timestamp : new Date().getTime();
  198. }
  199.  
  200. function setUserName() {
  201. console.log("In setUserName.");
  202. $.ajax({
  203. type: "GET",
  204. headers: {"Authorization": localStorage.authtoken},
  205. url: HOST + URLS["userme"]
  206. }).done(function (data, status, xhr) {
  207. $(".sp-username").html(xhr.responseJSON.properties.username);
  208. }).fail(function (xhr, status, error) {
  209. $(".sp-username").html("");
  210. });
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement