Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var long;
  2. var lat;
  3. var places = []
  4. var recognition = new webkitSpeechRecognition();
  5. var latLong;
  6. var options = {
  7.   enableHighAccuracy: true,
  8.   timeout: 1000,
  9.   maximumAge: 0
  10. };
  11. var dataCollection = {};
  12.  
  13.  
  14. $(document).ready(function(){
  15. $.getJSON("http://ipinfo.io", function(ipinfo){
  16.   latLong = ipinfo.loc.split(",");
  17.   lat = latLong[0]
  18.   long = latLong[1]
  19.   var map = new google.maps.Map(document.getElementById('map'), {
  20.     zoom: 13,
  21.     center: {lat: parseFloat(lat), lng: parseFloat(long)}
  22.   });
  23. }).fail(function (jqxhr, status, error) {
  24.   navigator.geolocation.getCurrentPosition(success)
  25. });
  26. })
  27.  
  28. function success(pos) {
  29.   var crd = pos.coords;
  30.   lat = crd.latitude
  31.   long = crd.longitude
  32.   var map = new google.maps.Map(document.getElementById('map'), {
  33.     zoom: 13,
  34.     center: {lat: parseFloat(lat), lng: parseFloat(long)}
  35.   });
  36.  
  37. };
  38.  
  39. function error(err) {
  40.   console.warn(`ERROR(${err.code}): ${err.message}`);
  41. };
  42.  
  43. $('.blue').click(function() {
  44.   listen();
  45.   $(this).toggleClass('blue green')
  46. });
  47.  
  48. function listen() {
  49.   recognition.continuous = false;
  50.  
  51.   recognition.onresult = function(event) {
  52.  
  53.     var speech = event.results[event.results.length-1][0].transcript;
  54.  
  55.     $('.textSpoken').text(speech);
  56.  
  57.     searchAPI(speech);
  58.   }
  59.   recognition.start();
  60.  
  61.   recognition.onend = function(){
  62.     $(".green").toggleClass('green blue')
  63.   }
  64. }
  65.  
  66. function searchAPI(speech) {
  67.   var found = false;
  68.   var wordArray = speech.split(" ");
  69.  
  70.   for (q = 0; q < wordArray.length; q++) {
  71.     wordArray[q] = changeCase(wordArray[q])
  72.   }
  73.  
  74.   $.getJSON("data.json", function(json) {
  75.     for (i = 0; i < json.cuisines.length; i++) {
  76.       for (k = 0; k < wordArray.length; k++) {
  77.         if (json.cuisines[i].cuisine.cuisine_name == wordArray[k]) {
  78.           found = true;
  79.           var foodType = json.cuisines[i].cuisine.cuisine_name
  80.           var typeID = json.cuisines[i].cuisine.cuisine_id
  81.  
  82.           dataCollection.foodType = json.cuisines[i].cuisine.cuisine_name
  83.           dataCollection.typeID = json.cuisines[i].cuisine.cuisine_id
  84.           dataCollection.long = long;
  85.           dataCollection.lat = lat;
  86.           var url = 'https://developers.zomato.com/api/v2.1/search?count=10&lat=' + dataCollection.lat + "&lon=" + dataCollection.long + "&cuisines=" + dataCollection.typeID
  87.           $.ajax({
  88.             type: 'GET',
  89.             url: url,
  90.             dataType: 'json',
  91.             headers: { 'user-key': '84a04de46614f9461a3a0d4e8b806efe' },
  92.             success: function(data){
  93.               $('.table').find("td").remove();
  94.               places = [];
  95.               if (data.restaurants.length > 0) {
  96.                 for (p = 0; p < data.restaurants.length; p++) {
  97.                   places.push({name : data.restaurants[p].restaurant.name, address:  data.restaurants[p].restaurant.location.address, long:  parseFloat(data.restaurants[p].restaurant.location.longitude),  lat:  parseFloat(data.restaurants[p].restaurant.location.latitude)})
  98.                   $('tbody').append("<tr><td class='top aligned'>" + data.restaurants[p].restaurant.name + "<br>" + data.restaurants[p].restaurant.location.address)
  99.                 }
  100.               } else {
  101.                 $('tbody').append("<tr><td class='top aligned'>No '" + dataCollection.foodType + "' in the area.</td></tr>")
  102.  
  103.               }
  104.               var map = new google.maps.Map(document.getElementById('map'), {
  105.                 zoom: 12,
  106.                 center: {lat: parseFloat(lat), lng: parseFloat(long)}
  107.               });
  108.               for (r = 0; r < places.length; r++)  {
  109.                 var latLng = new google.maps.LatLng(places[r].lat, places[r].long);
  110.                 var contentString = places[r].name + "<br>" + places[r].address
  111.  
  112.                 marker = new google.maps.Marker({
  113.                   position: latLng,
  114.                   map: map,
  115.                   contentString: contentString
  116.                 });
  117.  
  118.                 var infowindow = new google.maps.InfoWindow({});
  119.  
  120.                 marker.addListener('click', function() {
  121.                   infowindow.setContent(this.contentString);
  122.                   infowindow.open(map, this);
  123.                   map.setCenter(this.getPosition());
  124.                 });
  125.               }
  126.             }
  127.           })
  128.         }
  129.     }
  130.     }
  131.     if (!found) {
  132.       $('.ui.modal')
  133.     .modal('show');
  134.     $('.textSpoken').text('');
  135.     places = [];
  136.     $('.table').find("td").remove();
  137.     $('tbody').append("<tr><td class='top aligned'>None</td></tr>")
  138.  
  139.  
  140.  
  141.     }
  142.   }) .fail(function (jqxhr, status, error) {
  143.     console.log('error', status, error) }
  144.   );
  145. }
  146.  
  147. function changeCase(string) {
  148.   //written by user on stack overflow
  149.   var splitStr = string.toLowerCase().split(' ');
  150.   for (var i = 0; i < splitStr.length; i++) {
  151.     splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
  152.   }
  153.   return splitStr.join(' ');
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement