geolocation.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. // Constants
  5. const MapsViewElementID = "mapContainer";
  6. const InfoTextElementID = "informationText";
  7. const DefaultLatitude = 52.531;
  8. const DefaultLongitude = 13.385;
  9. // Change the following constant value to setStaticMap if you want to use
  10. // a static map instead of the pannable map.
  11. const SetMapFunction = setPannableMap;
  12. // Position options for Geolocation API
  13. var positionOptions = {
  14. enableHighAccuracy: false, // Low accuracy is sufficient by default
  15. timeout: 30000, // 30 seconds
  16. maximumAge: 0 // Don't accept cached location
  17. }
  18. // For pannable map
  19. var map;
  20. var marker;
  21. var lastPosition = [ DefaultLatitude, DefaultLongitude ];
  22. /**
  23. * Called on startup.
  24. */
  25. function init()
  26. {
  27. loadPannableMap();
  28. getPosition(SetMapFunction);
  29. }
  30. /**
  31. * Sets the information text in the HTML context.
  32. *
  33. * @param text The text to set.
  34. */
  35. function setInformationText(text)
  36. {
  37. var informationTextElement = document.getElementById(InfoTextElementID);
  38. informationTextElement.innerHTML = text;
  39. }
  40. /**
  41. * Tries to get the current position of the user using the Geolocation API.
  42. *
  43. * @param setMapFunction The function used for setting the map.
  44. */
  45. function getPosition(setMapFunction)
  46. {
  47. if (navigator && navigator.geolocation) {
  48. navigator.geolocation.getCurrentPosition(setMapFunction,
  49. onError,
  50. positionOptions);
  51. setInformationText("Retrieving location...");
  52. }
  53. else {
  54. alert("No Geolocation API available!");
  55. setInformationText("Unable to retrieve location!");
  56. setMapFunction({ coords: {
  57. latitude: DefaultLatitude,
  58. longitude: DefaultLongitude }});
  59. }
  60. }
  61. /**
  62. * Loads the map according to the given position.
  63. *
  64. * @param position The position received from the Geolocation API.
  65. */
  66. function setStaticMap(position)
  67. {
  68. setInformationText("Location found!");
  69. // Get the maps view element.
  70. var mapsViewElement = document.getElementById(MapsViewElementID);
  71. // Load the map and set it as the background image of the maps view
  72. // element.
  73. var url = new StaticMap({
  74. center: { lat: position.coords.latitude,
  75. lon: position.coords.longitude },
  76. zoom: 12,
  77. size: { width: 640, height: 360 }
  78. }).getCSSUrl();
  79. mapsViewElement.style.backgroundImage = url;
  80. }
  81. /**
  82. * Creates a pannable map.
  83. */
  84. function loadPannableMap()
  85. {
  86. setInformationText("Loading map...");
  87. map = new ovi.mapsapi.map.Display(
  88. document.getElementById(MapsViewElementID),
  89. { components: [
  90. // Uncomment any components you wish to add into your map.
  91. new ovi.mapsapi.map.component.Behavior()//,
  92. //new ovi.mapsapi.map.component.ZoomBar(),
  93. //new ovi.mapsapi.map.component.Overview(),
  94. //new ovi.mapsapi.map.component.TypeSelector(),
  95. //new ovi.mapsapi.map.component.ScaleBar()
  96. ],
  97. zoomLevel: 8,
  98. center: [DefaultLatitude, DefaultLongitude]
  99. });
  100. }
  101. /**
  102. * Centers the map on the given position and add a marker.
  103. *
  104. * @param position The position received from the Geolocation API.
  105. */
  106. function setPannableMap(position)
  107. {
  108. setInformationText("Location found!");
  109. // Store the position.
  110. lastPosition = position.coords;
  111. // Center the map.
  112. map.setCenter( [ position.coords.latitude, position.coords.longitude ] );
  113. // Create and add the marker.
  114. marker = new ovi.mapsapi.map.StandardMarker(
  115. [position.coords.latitude, position.coords.longitude],
  116. {
  117. text: "Me",
  118. draggable: false
  119. });
  120. // Uncomment the following to use a custom marker.
  121. //marker.set("icon", "images/marker.png");
  122. map.objects.add(marker);
  123. }
  124. /**
  125. * Callback for handling errors. Displays the error message in the HTML
  126. * context.
  127. *
  128. * @param error The occured error.
  129. */
  130. function onError(error)
  131. {
  132. setInformationText("Error: " + error.message);
  133. }
  134. /**
  135. * Sets the accuracy according to the given argument. Tries to update the
  136. * position automatically.
  137. *
  138. * @param accuracy Should be "high" or "low" depending on the desired accuracy.
  139. */
  140. function setAccuracy(accuracy)
  141. {
  142. if (accuracy == "high") {
  143. setInformationText("Accuracy set: high");
  144. positionOptions.enableHighAccuracy = false;
  145. }
  146. else {
  147. setInformationText("Accuracy set: low");
  148. positionOptions.enableHighAccuracy = true;
  149. }
  150. getPosition(SetMapFunction);
  151. }
  152. /**
  153. * Centers the map. This has an effect only if the pannable map is in use.
  154. */
  155. function centerMap()
  156. {
  157. if (map) {
  158. map.setCenter(lastPosition);
  159. }
  160. }
  161. /**
  162. * Updates the position.
  163. */
  164. function update()
  165. {
  166. getPosition(SetMapFunction);
  167. }
  168. // End of file.