Advertisement
Jodyone

services.js

May 10th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * service.js
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 8
  6.  *
  7.  * Implements a shuttle service.
  8.  */
  9.  
  10. // default height
  11. var HEIGHT = 0.8;
  12.  
  13. // default latitude
  14. var LATITUDE = 42.3745615030193;
  15.  
  16. // default longitude
  17. var LONGITUDE = -71.11803936751632;
  18.  
  19. // default heading
  20. var HEADING = 1.757197490907891;
  21.  
  22. // default number of seats
  23. var SEATS = 10;
  24.  
  25. // default velocity
  26. var VELOCITY = 50;
  27.  
  28. // global reference to shuttle's marker on 2D map
  29. var bus = null;
  30.  
  31. // global reference to 3D Earth
  32. var earth = null;
  33.  
  34. // global reference to 2D map
  35. var map = null;
  36.  
  37. // global reference to shuttle
  38. var shuttle = null;
  39.  
  40. // load version 1 of the Google Earth API
  41. google.load("earth", "1");
  42.  
  43. // load version 3 of the Google Maps API
  44. google.load("maps", "3", {other_params: "sensor=false"});
  45.  
  46. // once the window has loaded
  47. $(window).load(function() {
  48.  
  49.     // listen for keydown anywhere in body
  50.     $(document.body).keydown(function(event) {
  51.         return keystroke(event, true);
  52.     });
  53.  
  54.     // listen for keyup anywhere in body
  55.     $(document.body).keyup(function(event) {
  56.         return keystroke(event, false);
  57.     });
  58.  
  59.     // listen for click on Drop Off button
  60.     $("#dropoff").click(function(event) {
  61.         dropoff();
  62.     });
  63.  
  64.     // listen for click on Pick Up button
  65.     $("#pickup").click(function(event) {
  66.         pickup();
  67.     });
  68.  
  69.     // load application
  70.     load();
  71. });
  72.  
  73. // unload application
  74. $(window).unload(function() {
  75.     unload();
  76. });
  77.  
  78. /**
  79.  * Renders seating chart.
  80.  */
  81. function chart()
  82. {
  83.  
  84.     var html = "<ol start='0'>";
  85.     for (var j = 0; j < shuttle.seats.length; j++)
  86.     {
  87.            
  88.         if (shuttle.seats[j] == null)
  89.         {
  90.            
  91.             html += "<li>Empty Seat</li>";
  92.         }
  93.         else
  94.         {
  95.            
  96.            
  97.             html += "<li>" + shuttle.seats[j].html + "</li>";
  98.         }
  99.     }
  100.     html += "</ol>";
  101.     $("#chart").html(html);
  102. }
  103.  
  104. /**
  105.  * Drops off passengers if their stop is nearby.
  106.  */
  107. function dropoff()
  108. {
  109.      
  110.         for (var j = 0; j < shuttle.seats.length; j++)
  111.         {
  112.            if (shuttle.seats[j].x != null)
  113.            {
  114.                 var house = shuttle.seats[j].x;
  115.            
  116.                 var d_from_h = shuttle.distance(HOUSES[house].lat,HOUSES[house].lng);
  117.                
  118.                 if ( d_from_h < 30.0)
  119.                 {
  120.            
  121.                     shuttle.seats[j] = null;  
  122.              
  123.                 }
  124.             }
  125.    
  126.         }
  127.  
  128.     chart();
  129. }
  130.  
  131. /**
  132.  * Called if Google Earth fails to load.
  133.  */
  134. function failureCB(errorCode)
  135. {
  136.     // report error unless plugin simply isn't installed
  137.     if (errorCode != ERR_CREATE_PLUGIN)
  138.     {
  139.         alert(errorCode);
  140.     }
  141. }
  142.  
  143. /**
  144.  * Handler for Earth's frameend event.
  145.  */
  146. function frameend()
  147. {
  148.     shuttle.update();
  149. }
  150.  
  151. /**
  152.  * Called once Google Earth has loaded.
  153.  */
  154. function initCB(instance)
  155. {
  156.     // retain reference to GEPlugin instance
  157.     earth = instance;
  158.  
  159.     // specify the speed at which the camera moves
  160.     earth.getOptions().setFlyToSpeed(100);
  161.  
  162.     // show buildings
  163.     earth.getLayerRoot().enableLayerById(earth.LAYER_BUILDINGS, true);
  164.  
  165.     // disable terrain (so that Earth is flat)
  166.     earth.getLayerRoot().enableLayerById(earth.LAYER_TERRAIN, false);
  167.  
  168.     // prevent mouse navigation in the plugin
  169.     earth.getOptions().setMouseNavigationEnabled(false);
  170.  
  171.     // instantiate shuttle
  172.     shuttle = new Shuttle({
  173.         heading: HEADING,
  174.         height: HEIGHT,
  175.         latitude: LATITUDE,
  176.         longitude: LONGITUDE,
  177.         planet: earth,
  178.         seats: SEATS,
  179.         velocity: VELOCITY
  180.     });
  181.  
  182.     // synchronize camera with Earth
  183.     google.earth.addEventListener(earth, "frameend", frameend);
  184.  
  185.     // synchronize map with Earth
  186.     google.earth.addEventListener(earth.getView(), "viewchange", viewchange);
  187.  
  188.     // update shuttle's camera
  189.     shuttle.updateCamera();
  190.  
  191.     // show Earth
  192.     earth.getWindow().setVisibility(true);
  193.  
  194.     // render seating chart
  195.     chart();
  196.  
  197.     // populate Earth with passengers and houses
  198.     populate();
  199. }
  200.  
  201. /**
  202.  * Handles keystrokes.
  203.  */
  204. function keystroke(event, state)
  205. {
  206.     // ensure we have event
  207.     if (!event)
  208.     {
  209.         event = window.event;
  210.     }
  211.  
  212.     // left arrow
  213.     if (event.keyCode == 37)
  214.     {
  215.         shuttle.states.turningLeftward = state;
  216.         return false;
  217.     }
  218.  
  219.     // up arrow
  220.     else if (event.keyCode == 38)
  221.     {
  222.         shuttle.states.tiltingUpward = state;
  223.         return false;
  224.     }
  225.  
  226.     // right arrow
  227.     else if (event.keyCode == 39)
  228.     {
  229.         shuttle.states.turningRightward = state;
  230.         return false;
  231.     }
  232.  
  233.     // down arrow
  234.     else if (event.keyCode == 40)
  235.     {
  236.         shuttle.states.tiltingDownward = state;
  237.         return false;
  238.     }
  239.  
  240.     // A, a
  241.     else if (event.keyCode == 65 || event.keyCode == 97)
  242.     {
  243.         shuttle.states.slidingLeftward = state;
  244.         return false;
  245.     }
  246.  
  247.     // D, d
  248.     else if (event.keyCode == 68 || event.keyCode == 100)
  249.     {
  250.         shuttle.states.slidingRightward = state;
  251.         return false;
  252.     }
  253.  
  254.     // S, s
  255.     else if (event.keyCode == 83 || event.keyCode == 115)
  256.     {
  257.         shuttle.states.movingBackward = state;    
  258.         return false;
  259.     }
  260.  
  261.     // W, w
  262.     else if (event.keyCode == 87 || event.keyCode == 119)
  263.     {
  264.         shuttle.states.movingForward = state;    
  265.         return false;
  266.     }
  267.  
  268.     return true;
  269. }
  270.  
  271. /**
  272.  * Loads application.
  273.  */
  274. function load()
  275. {
  276.     // embed 2D map in DOM
  277.     var latlng = new google.maps.LatLng(LATITUDE, LONGITUDE);
  278.     map = new google.maps.Map($("#map").get(0), {
  279.         center: latlng,
  280.         disableDefaultUI: true,
  281.         mapTypeId: google.maps.MapTypeId.ROADMAP,
  282.         scrollwheel: false,
  283.         zoom: 17,
  284.         zoomControl: true
  285.     });
  286.  
  287.     // prepare shuttle's icon for map
  288.     bus = new google.maps.Marker({
  289.         icon: "https://maps.gstatic.com/intl/en_us/mapfiles/ms/micons/bus.png",
  290.         map: map,
  291.         title: "you are here"
  292.     });
  293.  
  294.     // embed 3D Earth in DOM
  295.     google.earth.createInstance("earth", initCB, failureCB);
  296. }
  297.  
  298. /**
  299.  * Picks up nearby passengers.
  300.  */
  301. function pickup()
  302. {
  303.    var features = earth.getFeatures();
  304.  
  305.    for (var i = 0; i < PASSENGERS.length; i++)
  306.    {
  307.        if (PASSENGERS[i].placemark != null)
  308.        {
  309.            var lat = PASSENGERS[i].placemark.getGeometry().getLatitude();
  310.            var lng = PASSENGERS[i].placemark.getGeometry().getLongitude();
  311.            var dist = shuttle.distance(lat,lng);
  312.          
  313.            if (dist < 15.0 )
  314.            {  
  315.              
  316.                 if (!HOUSES[PASSENGERS[i].house])
  317.                 {
  318.                      $("#announcements").html("NO FRESHMAN");
  319.                      break;
  320.                 }
  321.                 else
  322.                 {
  323.                    
  324.                     for(var j = 0; j < shuttle.seats.length; j++)
  325.                     {
  326.                        
  327.                        if (shuttle.seats[j] == null)
  328.                        {
  329.                            
  330.                          shuttle.seats[j] = {html:PASSENGERS[i].name + ' at ' + PASSENGERS[i].house,
  331.                                               x:PASSENGERS[i].house};
  332.                            features.removeChild(PASSENGERS[i].placemark);  
  333.                            PASSENGERS[i].marker.setMap(null);
  334.                            break;
  335.  
  336.                        
  337.                         }
  338.                         if (shuttle.seats[j] != null)
  339.                         {
  340.                              $("#announcements").html("NO SEATS AVAILABLE");
  341.                         }  
  342.  
  343.                }
  344.                }
  345.                    
  346.              
  347.            }
  348.            
  349.            else
  350.            {
  351.                $("#announcements").html("no passengers in range");
  352.            }  
  353.         }  
  354.    }
  355.    chart();
  356.      
  357. }
  358.  
  359. /**
  360.  * Populates Earth with passengers and houses.
  361.  */
  362. function populate()
  363. {
  364.     // mark houses
  365.     for (var house in HOUSES)
  366.     {
  367.         // plant house on map
  368.         new google.maps.Marker({
  369.             icon: "https://google-maps-icons.googlecode.com/files/home.png",
  370.             map: map,
  371.             position: new google.maps.LatLng(HOUSES[house].lat, HOUSES[house].lng),
  372.             title: house
  373.         });
  374.     }
  375.  
  376.     // get current URL, sans any filename
  377.     var url = window.location.href.substring(0, (window.location.href.lastIndexOf("/")) + 1);
  378.  
  379.     // scatter passengers
  380.     for (var i = 0; i < PASSENGERS.length; i++)
  381.     {
  382.         // pick a random building
  383.         var building = BUILDINGS[Math.floor(Math.random() * BUILDINGS.length)];
  384.  
  385.         // prepare placemark
  386.         var placemark = earth.createPlacemark("");
  387.         placemark.setName(PASSENGERS[i].name + " to " + PASSENGERS[i].house);
  388.  
  389.         // prepare icon
  390.         var icon = earth.createIcon("");
  391.         icon.setHref(url + "/img/" + PASSENGERS[i].username + ".jpg");
  392.  
  393.         // prepare style
  394.         var style = earth.createStyle("");
  395.         style.getIconStyle().setIcon(icon);
  396.         style.getIconStyle().setScale(4.0);
  397.  
  398.         // prepare stylemap
  399.         var styleMap = earth.createStyleMap("");
  400.         styleMap.setNormalStyle(style);
  401.         styleMap.setHighlightStyle(style);
  402.  
  403.         // associate stylemap with placemark
  404.         placemark.setStyleSelector(styleMap);
  405.  
  406.         // prepare point
  407.         var point = earth.createPoint("");
  408.         point.setAltitudeMode(earth.ALTITUDE_RELATIVE_TO_GROUND);
  409.         point.setLatitude(building.lat);
  410.         point.setLongitude(building.lng);
  411.         point.setAltitude(0.0);
  412.  
  413.         // associate placemark with point
  414.         placemark.setGeometry(point);
  415.  
  416.         // add placemark to Earth
  417.         earth.getFeatures().appendChild(placemark);
  418.  
  419.         // add marker to map
  420.         var marker = new google.maps.Marker({
  421.             icon: "https://maps.gstatic.com/intl/en_us/mapfiles/ms/micons/man.png",
  422.             map: map,
  423.             position: new google.maps.LatLng(building.lat, building.lng),
  424.             title: PASSENGERS[i].name + " at " + building.name
  425.         });
  426.  
  427.         // TODO: remember passenger's placemark and marker for pick-up's sake
  428.         PASSENGERS[i].placemark = placemark;
  429.         PASSENGERS[i].marker = marker;
  430.  
  431.     }
  432.  
  433. }
  434.  
  435. /**
  436.  * Handler for Earth's viewchange event.
  437.  */
  438. function viewchange()
  439. {
  440.     // keep map centered on shuttle's marker
  441.     var latlng = new google.maps.LatLng(shuttle.position.latitude, shuttle.position.longitude);
  442.     map.setCenter(latlng);
  443.     bus.setPosition(latlng);
  444. }
  445.  
  446. /**
  447.  * Unloads Earth.
  448.  */
  449. function unload()
  450. {
  451.     google.earth.removeEventListener(earth.getView(), "viewchange", viewchange);
  452.     google.earth.removeEventListener(earth, "frameend", frameend);
  453. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement