// Start by setting your token from https://developer.magiclane.com/api/projects if(gem.core.App.token===undefined) gem.core.App.token=""; function activateLocation() { // View of the map var defaultView = gem.core.App.getDefaultScreen().getDefaultMapView(); // Set the camera to follow the simulated position of the green arrow along the route on the map // when the blue arrow icon is clicked; panning or clicking on the map during the simulation // turns off following position, and here it is turned back on upon clicking the blue arrow icon defaultView.startFollowingPosition(); } // Draw the next turn symbol to the left of the instructions in the top panel function setCanvas(image_data, width, height, canvas_name) { var canvas = document.getElementById(canvas_name); if (canvas) { var imageData = new ImageData(new Uint8ClampedArray(image_data), width, height); canvas.width = width; canvas.height = height; canvas.getContext('2d').putImageData(imageData, 0, 0); } else { console.error('Canvas name not valid'); } } // The main function gem.core.App.registerInitialCallFunction(function() { // View of the map var defaultView = gem.core.App.getDefaultScreen().getDefaultMapView(); // Sample departure point and destination coordinates for a route var departureCoordinates = {latitude: 37.7749,longitude: -122.4194, altitude: 0, bearing: 0.0}; var destinationCoordinates = {latitude: 38.5816,longitude: -121.4944, altitude: 0, bearing: 0.0}; var defaultRoute = new gem.routesAndNavigation.RoutesRequest(); defaultRoute.addWaypoint(departureCoordinates); defaultRoute.addWaypoint(destinationCoordinates); defaultRoute.setBuildTerrainProfile(false); // This function is called when calculateRoute completes var callbackFunction = function() { var msecFlightDuration = 3000; // Draw the computed route on the map defaultView.showRouteInView(defaultRoute.get(0),true); // Fly the camera to the result route bounding box/area, with flight duration in milliseconds defaultView.centerOnRoute(defaultRoute.get(0),msecFlightDuration); var navigationInstructionEM = new gem.routesAndNavigation.NavigationListener(); // Function to write navigation instructions and statistics in their respective panels on the map var navInstructionUpdate = function() { // Image of the next turn displayed to the left of the instructions var bitmapContainer = new gem.core.BitmapContainer(60,60); gem.routesAndNavigation.Navigation.getNextTurnImageInBitmap(bitmapContainer); setCanvas(bitmapContainer.toImageData(),60,60,"myCanvas"); var distancetoNext = gem.routesAndNavigation.Navigation.getTimeDistanceToNextTurn().totaldistance; var unit=" meters " if (distancetoNext>1000) { distancetoNext = distancetoNext/1000.0; unit = " Km "; } // Update the instructions, time and distance remaining, in the top and bottom panels document.getElementById('navigationText').innerHTML = "
In " + distancetoNext.toFixed(2) + unit + gem.routesAndNavigation.Navigation.getNextTurnInstruction() + "
"; var remainedTimeDistance = gem.routesAndNavigation.Navigation.getRemainingTravelTimeDistance(); document.getElementById('bottomPanelRemainingDistance').innerHTML = (remainedTimeDistance.totaldistance/1000).toFixed(2) + "km"; var measuredTime = new Date(null); measuredTime.setSeconds(remainedTimeDistance.totaltime); // specify value in seconds var MHSTime = measuredTime.toISOString().substr(11, 5); document.getElementById('bottomPanelRemainingTime').innerHTML = MHSTime+"hr"; bitmapContainer.delete(); }; navigationInstructionEM.registerNavInstructionUpdateListener(navInstructionUpdate); var progressListener = function() { // Set the camera to follow the simulated position of the green arrow along the route on the map defaultView.startFollowingPosition(); }; // Make the navigation instructions and statistics panels visible when navigation starts var navpanel = document.getElementById('navigationPanel'); var bottomPanel = document.getElementById('bottomPanel'); // Start simulated navigation after the fly to the route is complete setTimeout(function() { navpanel.style.visibility='visible'; bottomPanel.style.visibility='visible'; gem.routesAndNavigation.Navigation.startSimulation(defaultRoute.get(0),progressListener, navigationInstructionEM); },msecFlightDuration); }; // Compute the defaultRoute from the starting point to the destination initialized above defaultRoute.calculateRoute(callbackFunction); }); // Initializes the app gem.core.App.initApp();