Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var geocoo = new google.maps.Geocoder();
  2. var fenway = new google.maps.LatLng(-37.793237, 175.303775);
  3. var botswana = new google.maps.LatLng(-20.472393999999998, 24.515585999999995);
  4. var NewYork = new google.maps.LatLng(40.753497, -73.996356);
  5. var sandy = new google.maps.LatLng(40.554113, -74.110851);
  6. var sanFran = new google.maps.LatLng(37.775679, -122.425017);
  7. var HMSOcelot = new google.maps.LatLng(51.395492, 0.526726);
  8. var places = {
  9.     hamilton: fenway,
  10.     botswana: botswana,
  11.     newyork: NewYork,
  12.     sandy: sandy,
  13.     sanFran: sanFran,
  14.     submarine: HMSOcelot
  15. }
  16.  
  17. var defaultStart = NewYork;
  18.  
  19. $.urlParam = function(name)
  20. {
  21.     var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
  22.     if (results == null)
  23.     {
  24.         return null;
  25.     }
  26.     else
  27.     {
  28.         return results[1] || 0;
  29.     }
  30. }
  31.  
  32. function appendStatusDisplay(message)
  33. {
  34.     var text = $("#statusDisplay").html();
  35.     text += "<br>" + message;
  36.     $("#statusDisplay").html(text);
  37. }
  38.  
  39. var startLocation;
  40. var dataService;
  41. var panorama;
  42. var list;
  43. var place = $.urlParam('place');
  44.  
  45. function initialize()
  46. {
  47.     dataService = new google.maps.StreetViewService();
  48.     if (places[place])
  49.     {
  50.         startLocation = places[place];
  51.         run();
  52.     }
  53.     else if (!!place)
  54.     {
  55.         geocoo.geocode({ address: decodeURIComponent(place) }, function(data, status)
  56.         {
  57.             console.log(data);
  58.             if (!!data[0])
  59.             {
  60.                 startLocation = data[0].geometry.location;
  61.             }
  62.             else
  63.             {
  64.                 startLocation = defaultStart;
  65.             }
  66.             run();
  67.         });
  68.     }
  69.     else
  70.     {
  71.         startLocation = defaultStart;
  72.         run();
  73.     }
  74. }
  75.  
  76. function run()
  77. {
  78.     var panoramaOptions = {
  79.         position: startLocation,
  80.         pov: {
  81.             heading: 90,
  82.             pitch: 10,
  83.             zoom: 1
  84.         },
  85.         linksControl: false,
  86.         panControl: false,
  87.         disableDefaultUI: true
  88.     };
  89.     panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'), panoramaOptions);
  90.     var map = new google.maps.Map(document.getElementById('map-canvas'));
  91.     map.setStreetView(panorama);
  92.  
  93.     geocoo.geocode({ address: decodeURIComponent(startLocation) }, function(data, status)
  94.     {
  95.         appendStatusDisplay(data[0].address_components[0].short_name + " " + data[0].address_components[1].short_name);
  96.         appendStatusDisplay(startLocation);
  97.     });
  98.    
  99.     google.maps.event.addListener(panorama, "pov_changed", function()
  100.     {
  101.         console.log("pov_changed, doStreetView");
  102.         SyncThreeCamera(panorama.getPov().heading, panorama.getPov().pitch);
  103.     });
  104.  
  105.     google.maps.event.addListener(panorama, "links_changed", function()
  106.     {
  107.         console.log("nuking spheres and creating paths");
  108.         nukeSpheres();
  109.         if (list === undefined)
  110.         {
  111.             list = new PanoListWrapper(new PanoNode(panorama.pano, panorama.getPosition(), panorama.links, panorama.centerHeading));
  112.         }
  113.         list.naiveDisplay(10, list.currentPano);
  114.     });
  115.     google.maps.event.addListener(panorama, "zoom_changed", function()
  116.     {
  117.         camera = cameraFromGoogleZoom(panorama.getZoom());
  118.         SyncThreeCamera(panorama.getPov().heading, panorama.getPov().pitch)
  119.     });
  120. }
  121.  
  122. function PanoListWrapper(firstNode)
  123. {
  124.     var hashedNodeList = {};
  125.     hashedNodeList[firstNode.panoID] = firstNode;
  126.     this.currentPano = firstNode;
  127.  
  128.     this.getNodeList = function()
  129.     {
  130.         return hashedNodeList;
  131.     }
  132.  
  133.     this.setCurrentPano = function(newPano)
  134.     {
  135.         currentPano = newPano;
  136.         if(!hashedNodeList.hasOwnProperty(newPano.panoID))
  137.         {//probably not needed, but doesn't hurt to be safe
  138.             hashedNodeList[newPano.panoID] = newPano;
  139.         }
  140.     }
  141.  
  142.     this.naiveDisplay = function(count, startNode)
  143.     {//diplay count(integer) nodes along each direction(startNode.getLinks())
  144.         //var startNodeLinks = startNode.getLinks();
  145.         //console.log(startNodeLinks);
  146.         function waitForLinks()
  147.         {
  148.             if(startNode.getLinks()===undefined)
  149.             {
  150.                 window.setTimeout(waitForLinks, 50);
  151.             }
  152.             else
  153.             {
  154.                 resume();
  155.             }
  156.         }
  157.        
  158.         function resume()
  159.         {
  160.             for (pathStartID in startNode.getLinks())
  161.             {
  162.                 var currentStep = hashedNodeList[pathStartID];
  163.                 var newStep = currentStep;
  164.                 for (var i = 0; i < count; i++)
  165.                 {
  166.                    
  167.                     function resumeDraw()
  168.                     {
  169.                         createPanoSphere(google.maps.geometry.spherical.computeHeading(startNode.position, currentStep.position),
  170.                                             google.maps.geometry.spherical.computeDistanceBetween(startNode.position, currentStep.position),
  171.                                             currentStep,
  172.                                             currentStep.centerHeading);
  173.                         console.log("debug, forwardNode source1");
  174.                         newStep = currentStep.getNextForwardNode(startNode.getLinks()[pathStartID].heading);
  175.                     }
  176.                    
  177.                     function waitForStep()
  178.                     {
  179.                         if (newStep === undefined)
  180.                         {
  181.                             console.log("debug, forwardNode source2");
  182.                             console.log(newStep);
  183.                             newStep = currentStep.getNextForwardNode(startNode.getLinks()[pathStartID].heading);
  184.                             console.log(newStep);
  185.                             window.setTimeout(waitForStep, 50);
  186.                         }
  187.                         else
  188.                         {
  189.                             console.log("debug, foward node found:");
  190.                             console.log(newStep);
  191.                             currentStep = newStep;
  192.                             resumeDraw();
  193.                         }
  194.                     }
  195.                     waitForStep();
  196.                 }
  197.             }
  198.         }
  199.         waitForLinks();
  200.     }
  201. }
  202.  
  203. function PanoNode(panoID, position, newLinks, centerHeading)
  204. {
  205.     this.sphere;
  206.     this.panoID = panoID;
  207.     this.position = position;
  208.     this.links = {};
  209.     this.centerHeading = centerHeading;
  210.     for (var i = 0; i < newLinks.length; i++)
  211.     {
  212.         this.links[newLinks[i].pano] = null;
  213.     }
  214. }
  215.  
  216. PanoNode.prototype.getLinks = function()
  217. {//gets all links from this node
  218.     //    pulling them from Google if they are not locally available
  219.     // or
  220.     //    setting pointers to the correct location if they have already been pulled to local previously
  221.  
  222.     var waitingForData = 0;
  223.  
  224.     function decrementWaiting()
  225.     {
  226.         waitingForData--;
  227.         if (waitingForData > 0)
  228.         {
  229.         }
  230.         else
  231.         {
  232.             return this.links;
  233.         }
  234.     }
  235.  
  236.     for (linkID in this.links)
  237.     {
  238.         if (this.links[linkID] === null)
  239.         {
  240.             if (list.getNodeList.hasOwnProperty(linkID))
  241.             {//node to link to already exists locally
  242.                 var linkDistance = google.maps.geometry.spherical.computeDistanceBetween(this.position, list.getNodeList[linkID].position);
  243.                 var linkHeading = google.maps.geometry.spherical.computeHeading(this.position, list.getNodeList[linkID].position);
  244.                 this.links[linkID] = new PanoLink(linkDistance, linkHeading, linkID, list.getNodeList[linkID]);
  245.             }
  246.             else
  247.             {
  248.                 waitingForData++;
  249.                 this.links[linkID]=-1;
  250.                 (function(position, linkID, links)
  251.                 {
  252.                     dataService.getPanoramaById(linkID, function(svData, svStatus)
  253.                     {
  254.                         if (svStatus != "OK")
  255.                         {
  256.                             alert("svStatus not OK, unable to extend node graph\nCheck connection and Google server status");
  257.                         }
  258.                         var linkDistance = google.maps.geometry.spherical.computeDistanceBetween(position, svData.location.latLng);
  259.                         var linkHeading = google.maps.geometry.spherical.computeHeading(position, svData.location.latLng);
  260.                         links[linkID] = new PanoLink(linkDistance, linkHeading, linkID, new PanoNode(linkID, svData.location.latLng, svData.links, svData.tiles.centerHeading));
  261.                         decrementWaiting();
  262.                     });
  263.                 }(this.position, linkID, this.links));
  264.             }
  265.         }
  266.         else if (this.links[linkID] === -1)
  267.         {
  268.             waitingForData++;
  269.         }
  270.     }
  271.     if (waitingForData > 0)
  272.     {
  273.     }
  274.     else
  275.     {
  276.         return this.links;
  277.     }
  278. }
  279.  
  280. PanoNode.prototype.getVisibleLinks = function(heading, viewAngle)//using view angle as distance from center
  281. {//gets the links the kinect is able to select from
  282.     var visibleLinks = {}
  283.     var allLinks = this.getLinks;
  284.     for (linkID in Object.keys(allLinks))
  285.     {
  286.         if(allLinks[linkID].heading>viewAngle+heading||allLinks[linkID].heading<=viewAngle+heading)
  287.         {
  288.             visibleLinks[linkID] = allLinks[linkID];
  289.         }
  290.     }
  291.     return visibleLinks;
  292. }
  293.  
  294. PanoNode.prototype.getNextForwardNode = function(direction)
  295. {
  296.     var context = this;
  297.     var bestMatch;
  298.     var bestMatchValue=361;//heading in degrees, 361 should always be above maximum value
  299.     //var allLinks = this.getLinks();
  300.     function waitForLinks()
  301.     {
  302.         if (context.getLinks() === undefined)
  303.         {
  304.             window.setTimeout(waitForLinks, 50);
  305.         }
  306.         else
  307.         {
  308.             return resume();
  309.         }
  310.     }
  311.  
  312.     function resume()
  313.     {
  314.         for (currentLink in context.getLinks())
  315.         {
  316.             console.log("debug, findNextForward from " + currentLink);
  317.             var testCost = Math.abs(context.getLinks()[currentLink].heading - direction);
  318.             if(testCost<bestMatchValue)
  319.             {
  320.                 bestMatchValue=testCost;
  321.                 bestMatch=currentLink;
  322.             }
  323.         }
  324.         console.log("debug, findNextForward result " + bestMatch);
  325.         console.log(context.getLinks()[bestMatch].getNode());
  326.         return context.getLinks()[bestMatch].getNode();
  327.     }
  328.     waitForLinks();
  329. }
  330.  
  331. function PanoLink(distance, heading, linkID, newPanoNode)
  332. {
  333.     this.distance = distance;
  334.     this.heading = heading;
  335.     this.node = linkID;
  336.     if (!list.getNodeList().hasOwnProperty(linkID))
  337.     {
  338.         list.getNodeList()[linkID] = newPanoNode;
  339.     }
  340. }
  341.  
  342. PanoLink.prototype.getNode = function()
  343. {
  344.     return list.getNodeList()[this.node];
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement