geolocation.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. var MapsViewElementID = "mapContainer";
  2. var InfoTextElementID = "informationText";
  3. var InfoNoteElementID = "informationNote";
  4. var InfoContainerElementID = "infoContainer";
  5. var DefaultLatitude = 52.531;
  6. var DefaultLongitude = 13.385;
  7. // Change the following constant value to setStaticMap if you want to use
  8. // a static map instead of the pannable map.
  9. var mapFunction;
  10. //var mapFunction = setStaticMap;
  11. // Use Nokia or Bing Map
  12. var USE_NOKIA_MAP = true;
  13. var watchId;
  14. // Position options for Geolocation API
  15. var positionOptions = {
  16. enableHighAccuracy: false, // Low accuracy is sufficient by default
  17. timeout: 30000, // 30 seconds
  18. maximumAge: 0 // Don't accept cached location
  19. }
  20. // For pannable map
  21. var map;
  22. var marker;
  23. var lastPosition = [ DefaultLatitude, DefaultLongitude ];
  24. /**
  25. * Initialize map and position
  26. */
  27. function init()
  28. {
  29. // Use Nokia or Bing Maps
  30. if (USE_NOKIA_MAP) {
  31. mapFunction = setPannableNokiaMap;
  32. loadPannableNokiaMap();
  33. } else {
  34. mapFunction = setPannableBingMap;
  35. loadPannableBingMap();
  36. }
  37. // Get HTML5 Geolocation
  38. getPosition(mapFunction);
  39. }
  40. /**
  41. * Shows Windows Phone infomessage to user.
  42. */
  43. function showMessage(msg)
  44. {
  45. window.external.notify(msg);
  46. }
  47. /**
  48. * Shows HTML info text.
  49. */
  50. function setInformationText(text)
  51. {
  52. var informationTextElement = document.getElementById("informationText");
  53. informationTextElement.innerHTML = text;
  54. }
  55. /**
  56. * Sets the information text in the HTML context.
  57. *
  58. * @param text The text to set.
  59. */
  60. function setInformationText(text)
  61. {
  62. var informationNoteElement = document.getElementById(InfoNoteElementID);
  63. informationNoteElement.style.display='block';
  64. var informationTextElement = document.getElementById(InfoTextElementID);
  65. informationTextElement.innerHTML = text;
  66. }
  67. /**
  68. * Timer for hiding info text
  69. */
  70. function hideInformationText()
  71. {
  72. var timer = setTimeout("doHideInformationText()",3000);
  73. }
  74. /**
  75. * Hide info text
  76. */
  77. function doHideInformationText()
  78. {
  79. var informationNoteElement = document.getElementById(InfoNoteElementID);
  80. informationNoteElement.style.display='none';
  81. }
  82. /**
  83. * Show info
  84. */
  85. function showInfo()
  86. {
  87. var informationContainerElement = document.getElementById(InfoContainerElementID);
  88. informationContainerElement.style.display='block';
  89. }
  90. /**
  91. * Close info
  92. */
  93. function closeInfo()
  94. {
  95. var informationContainerElement = document.getElementById(InfoContainerElementID);
  96. informationContainerElement.style.display='none';
  97. }
  98. /*
  99. * Is info view visible
  100. */
  101. function isInfoVisible()
  102. {
  103. var informationContainerElement = document.getElementById(InfoContainerElementID);
  104. if (isVisible(informationContainerElement))
  105. return '1';
  106. else
  107. return '0';
  108. }
  109. /*
  110. * Is element visible
  111. */
  112. function isVisible(elem) {
  113. var cmpstyle = window.getComputedStyle(elem,null);
  114. if (parseFloat(cmpstyle.opacity) > 0 &&
  115. cmpstyle.visibility != 'hidden' &&
  116. cmpstyle.display != 'none') {
  117. return true;
  118. }
  119. return false;
  120. }
  121. /**
  122. * Tries to get the current position of the user using the Geolocation API.
  123. *
  124. * @param setMapFunction The function used for setting the map.
  125. */
  126. function getPosition(mapFunctionParameter)
  127. {
  128. if (navigator && navigator.geolocation) {
  129. setInformationText("Retrieving location...");
  130. navigator.geolocation.getCurrentPosition(mapFunctionParameter,
  131. onError,
  132. positionOptions);
  133. }
  134. else {
  135. alert("No Geolocation API available!");
  136. }
  137. }
  138. /**
  139. * Loads the map according to the given position.
  140. *
  141. * @param position The position received from the Geolocation API.
  142. */
  143. function setStaticMap(position)
  144. {
  145. hideInformationText();
  146. // Get the maps view element.
  147. var mapsViewElement = document.getElementById(MapsViewElementID);
  148. // Load the map and set it as the background image of the maps view
  149. // element.
  150. var url = new StaticMap({
  151. center: { lat: position.coords.latitude,
  152. lon: position.coords.longitude },
  153. zoom: 12,
  154. size: { width: document.width, height: document.height }
  155. }).getCSSUrl();
  156. mapsViewElement.style.backgroundImage = url;
  157. }
  158. /**
  159. * Creates a pannable map.
  160. * Nokia Map
  161. */
  162. function loadPannableNokiaMap()
  163. {
  164. setInformationText("Loading Nokia Maps...");
  165. map = new nokia.maps.map.Display(
  166. document.getElementById(MapsViewElementID), {
  167. components: [ new nokia.maps.map.component.Behavior()],
  168. 'zoomLevel': 12, // Zoom level for the map
  169. 'center': [DefaultLatitude, DefaultLongitude] // Center coordinates
  170. }
  171. );
  172. }
  173. /**
  174. * Creates a pannable map.
  175. * Bing Map
  176. */
  177. function loadPannableBingMap()
  178. {
  179. setInformationText("Loading Bing Maps...");
  180. map = new Microsoft.Maps.Map(document.getElementById(MapsViewElementID),
  181. {credentials: 'AtqCp27rVpTHmmf0iQjxS36z1YdD1RnucNIFt0g4Yz529GW3AuybYVBGb55EuMZi',
  182. mapTypeId: Microsoft.Maps.MapTypeId.road,
  183. showScalebar: false,
  184. disableZooming: true,
  185. center: new Microsoft.Maps.Location(DefaultLatitude, DefaultLongitude),
  186. enableClickableLogo: false,
  187. enableSearchLogo: false,
  188. showDashboard: false,
  189. zoom: 12});
  190. }
  191. /**
  192. * Centers the map on the given position and add a marker.
  193. * Nokia Maps
  194. *
  195. * @param position The position received from the Geolocation API.
  196. */
  197. function setPannableNokiaMap(position)
  198. {
  199. hideInformationText();
  200. // Store the position.
  201. lastPosition = position.coords;
  202. // Center the map.
  203. map.setCenter( [ position.coords.latitude, position.coords.longitude ] );
  204. // Create and add the marker.
  205. marker = new nokia.maps.map.Marker(
  206. [position.coords.latitude, position.coords.longitude],
  207. {
  208. text: "Me",
  209. draggable: false
  210. });
  211. // Uncomment the following to use a custom marker.
  212. //marker.set("icon", "images/marker.png");
  213. map.objects.clear();
  214. map.objects.add(marker);
  215. }
  216. /**
  217. * Centers the map on the given position and add a marker.
  218. * Bing Maps
  219. *
  220. * @param position The position received from the Geolocation API.
  221. */
  222. function setPannableBingMap(position)
  223. {
  224. hideInformationText();
  225. // Store the position.
  226. lastPosition = position.coords;
  227. //showMessage(position.coords.latitude + " " + position.coords.longitude);
  228. // Map center pos
  229. var location = new Microsoft.Maps.Location(position.coords.latitude,position.coords.longitude);
  230. map.setView({ zoom: 12, center: location });
  231. // Marker
  232. var pin = new Microsoft.Maps.Pushpin(location);
  233. map.entities.clear();
  234. map.entities.push(pin);
  235. // Request repeated updates
  236. //watchId = navigator.geolocation.watchPosition(mapFunction,
  237. // onError,
  238. // positionOptions);
  239. }
  240. /**
  241. * Callback for handling errors. Displays the error message in the HTML
  242. * context.
  243. *
  244. * @param error The occured error.
  245. */
  246. function onError(error)
  247. {
  248. var message = "Geolocation API error";
  249. hideInformationText();
  250. switch (error.code) {
  251. case error.PERMISSION_DENIED:
  252. message = "Enable device location and try again.";
  253. break;
  254. case error.POSITION_UNAVAILABLE:
  255. message = "The current position could not be determined.";
  256. break;
  257. case error.PERMISSION_DENIED_TIMEOUT:
  258. message = "The current position could not be determined " +
  259. "within the specified timeout period.";
  260. break;
  261. }
  262. showMessage(message);
  263. }
  264. /**
  265. * Sets the accuracy according to the given argument. Tries to update the
  266. * position automatically.
  267. *
  268. * @param accuracy Should be "high" or "low" depending on the desired accuracy.
  269. */
  270. function setAccuracy(accuracy)
  271. {
  272. if (accuracy == "high") {
  273. setInformationText("Accuracy set: high");
  274. positionOptions.enableHighAccuracy = false;
  275. }
  276. else {
  277. setInformationText("Accuracy set: low");
  278. positionOptions.enableHighAccuracy = true;
  279. }
  280. getPosition(mapFunction);
  281. }
  282. /**
  283. * Centers the map. This has an effect only if the pannable map is in use.
  284. */
  285. function centerMap()
  286. {
  287. if (map) {
  288. map.setCenter(lastPosition);
  289. }
  290. }
  291. /**
  292. * Updates the position.
  293. */
  294. function update()
  295. {
  296. setInformationText("Update...");
  297. getPosition(mapFunction);
  298. }