Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var long;
- var lat;
- var places = []
- var recognition = new webkitSpeechRecognition();
- var latLong;
- var options = {
- enableHighAccuracy: true,
- timeout: 1000,
- maximumAge: 0
- };
- var dataCollection = {};
- $(document).ready(function(){
- $.getJSON("http://ipinfo.io", function(ipinfo){
- latLong = ipinfo.loc.split(",");
- lat = latLong[0]
- long = latLong[1]
- var map = new google.maps.Map(document.getElementById('map'), {
- zoom: 13,
- center: {lat: parseFloat(lat), lng: parseFloat(long)}
- });
- }).fail(function (jqxhr, status, error) {
- navigator.geolocation.getCurrentPosition(success)
- });
- })
- function success(pos) {
- var crd = pos.coords;
- lat = crd.latitude
- long = crd.longitude
- var map = new google.maps.Map(document.getElementById('map'), {
- zoom: 13,
- center: {lat: parseFloat(lat), lng: parseFloat(long)}
- });
- };
- function error(err) {
- console.warn(`ERROR(${err.code}): ${err.message}`);
- };
- $('.blue').click(function() {
- listen();
- $(this).toggleClass('blue green')
- });
- function listen() {
- recognition.continuous = false;
- recognition.onresult = function(event) {
- var speech = event.results[event.results.length-1][0].transcript;
- $('.textSpoken').text(speech);
- searchAPI(speech);
- }
- recognition.start();
- recognition.onend = function(){
- $(".green").toggleClass('green blue')
- }
- }
- function searchAPI(speech) {
- var found = false;
- var wordArray = speech.split(" ");
- for (q = 0; q < wordArray.length; q++) {
- wordArray[q] = changeCase(wordArray[q])
- }
- $.getJSON("data.json", function(json) {
- for (i = 0; i < json.cuisines.length; i++) {
- for (k = 0; k < wordArray.length; k++) {
- if (json.cuisines[i].cuisine.cuisine_name == wordArray[k]) {
- found = true;
- var foodType = json.cuisines[i].cuisine.cuisine_name
- var typeID = json.cuisines[i].cuisine.cuisine_id
- dataCollection.foodType = json.cuisines[i].cuisine.cuisine_name
- dataCollection.typeID = json.cuisines[i].cuisine.cuisine_id
- dataCollection.long = long;
- dataCollection.lat = lat;
- var url = 'https://developers.zomato.com/api/v2.1/search?count=10&lat=' + dataCollection.lat + "&lon=" + dataCollection.long + "&cuisines=" + dataCollection.typeID
- $.ajax({
- type: 'GET',
- url: url,
- dataType: 'json',
- headers: { 'user-key': '84a04de46614f9461a3a0d4e8b806efe' },
- success: function(data){
- $('.table').find("td").remove();
- places = [];
- if (data.restaurants.length > 0) {
- for (p = 0; p < data.restaurants.length; p++) {
- 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)})
- $('tbody').append("<tr><td class='top aligned'>" + data.restaurants[p].restaurant.name + "<br>" + data.restaurants[p].restaurant.location.address)
- }
- } else {
- $('tbody').append("<tr><td class='top aligned'>No '" + dataCollection.foodType + "' in the area.</td></tr>")
- }
- var map = new google.maps.Map(document.getElementById('map'), {
- zoom: 12,
- center: {lat: parseFloat(lat), lng: parseFloat(long)}
- });
- for (r = 0; r < places.length; r++) {
- var latLng = new google.maps.LatLng(places[r].lat, places[r].long);
- var contentString = places[r].name + "<br>" + places[r].address
- marker = new google.maps.Marker({
- position: latLng,
- map: map,
- contentString: contentString
- });
- var infowindow = new google.maps.InfoWindow({});
- marker.addListener('click', function() {
- infowindow.setContent(this.contentString);
- infowindow.open(map, this);
- map.setCenter(this.getPosition());
- });
- }
- }
- })
- }
- }
- }
- if (!found) {
- $('.ui.modal')
- .modal('show');
- $('.textSpoken').text('');
- places = [];
- $('.table').find("td").remove();
- $('tbody').append("<tr><td class='top aligned'>None</td></tr>")
- }
- }) .fail(function (jqxhr, status, error) {
- console.log('error', status, error) }
- );
- }
- function changeCase(string) {
- //written by user on stack overflow
- var splitStr = string.toLowerCase().split(' ');
- for (var i = 0; i < splitStr.length; i++) {
- splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
- }
- return splitStr.join(' ');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement