123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- /**
- * Copyright (c) 2011 Nokia Corporation.
- */
- // Constants
- const MapsViewElementID = "mapContainer";
- const InfoTextElementID = "informationText";
- const DefaultLatitude = 52.531;
- const DefaultLongitude = 13.385;
- // Change the following constant value to setStaticMap if you want to use
- // a static map instead of the pannable map.
- const SetMapFunction = setPannableMap;
- // Position options for Geolocation API
- var positionOptions = {
- enableHighAccuracy: false, // Low accuracy is sufficient by default
- timeout: 30000, // 30 seconds
- maximumAge: 0 // Don't accept cached location
- }
- // For pannable map
- var map;
- var marker;
- var lastPosition = [ DefaultLatitude, DefaultLongitude ];
- /**
- * Called on startup.
- */
- function init()
- {
- loadPannableMap();
- getPosition(SetMapFunction);
- }
- /**
- * Sets the information text in the HTML context.
- *
- * @param text The text to set.
- */
- function setInformationText(text)
- {
- var informationTextElement = document.getElementById(InfoTextElementID);
- informationTextElement.innerHTML = text;
- }
- /**
- * Tries to get the current position of the user using the Geolocation API.
- *
- * @param setMapFunction The function used for setting the map.
- */
- function getPosition(setMapFunction)
- {
- if (navigator && navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(setMapFunction,
- onError,
- positionOptions);
- setInformationText("Retrieving location...");
- }
- else {
- alert("No Geolocation API available!");
- setInformationText("Unable to retrieve location!");
- setMapFunction({ coords: {
- latitude: DefaultLatitude,
- longitude: DefaultLongitude }});
- }
- }
- /**
- * Loads the map according to the given position.
- *
- * @param position The position received from the Geolocation API.
- */
- function setStaticMap(position)
- {
- setInformationText("Location found!");
- // Get the maps view element.
- var mapsViewElement = document.getElementById(MapsViewElementID);
- // Load the map and set it as the background image of the maps view
- // element.
- var url = new StaticMap({
- center: { lat: position.coords.latitude,
- lon: position.coords.longitude },
- zoom: 12,
- size: { width: 640, height: 360 }
- }).getCSSUrl();
- mapsViewElement.style.backgroundImage = url;
- }
- /**
- * Creates a pannable map.
- */
- function loadPannableMap()
- {
- setInformationText("Loading map...");
- map = new ovi.mapsapi.map.Display(
- document.getElementById(MapsViewElementID),
- { components: [
- // Uncomment any components you wish to add into your map.
- new ovi.mapsapi.map.component.Behavior()//,
- //new ovi.mapsapi.map.component.ZoomBar(),
- //new ovi.mapsapi.map.component.Overview(),
- //new ovi.mapsapi.map.component.TypeSelector(),
- //new ovi.mapsapi.map.component.ScaleBar()
- ],
- zoomLevel: 8,
- center: [DefaultLatitude, DefaultLongitude]
- });
- }
- /**
- * Centers the map on the given position and add a marker.
- *
- * @param position The position received from the Geolocation API.
- */
- function setPannableMap(position)
- {
- setInformationText("Location found!");
- // Store the position.
- lastPosition = position.coords;
- // Center the map.
- map.setCenter( [ position.coords.latitude, position.coords.longitude ] );
- // Create and add the marker.
- marker = new ovi.mapsapi.map.StandardMarker(
- [position.coords.latitude, position.coords.longitude],
- {
- text: "Me",
- draggable: false
- });
- // Uncomment the following to use a custom marker.
- //marker.set("icon", "images/marker.png");
- map.objects.add(marker);
- }
- /**
- * Callback for handling errors. Displays the error message in the HTML
- * context.
- *
- * @param error The occured error.
- */
- function onError(error)
- {
- setInformationText("Error: " + error.message);
- }
- /**
- * Sets the accuracy according to the given argument. Tries to update the
- * position automatically.
- *
- * @param accuracy Should be "high" or "low" depending on the desired accuracy.
- */
- function setAccuracy(accuracy)
- {
- if (accuracy == "high") {
- setInformationText("Accuracy set: high");
- positionOptions.enableHighAccuracy = false;
- }
- else {
- setInformationText("Accuracy set: low");
- positionOptions.enableHighAccuracy = true;
- }
- getPosition(SetMapFunction);
- }
- /**
- * Centers the map. This has an effect only if the pannable map is in use.
- */
- function centerMap()
- {
- if (map) {
- map.setCenter(lastPosition);
- }
- }
- /**
- * Updates the position.
- */
- function update()
- {
- getPosition(SetMapFunction);
- }
- // End of file.
|