From 1d6e2b3a0b4b986a5e3e39b1c239b6e56a64b110 Mon Sep 17 00:00:00 2001 From: Andrew Cafourek Date: Fri, 23 Aug 2013 14:26:35 -0700 Subject: [PATCH 1/3] Switch to Search API for local results Changed API from suggestCompletion to Search and added required parameters as well as HTML markup around results list. --- foursquare_suggest.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/foursquare_suggest.js b/foursquare_suggest.js index 6301a75..592d445 100644 --- a/foursquare_suggest.js +++ b/foursquare_suggest.js @@ -13,10 +13,12 @@ $.fn.fs_suggest = function(options) { var defaults = { - url : 'https://api.foursquare.com/v2/venues/suggestCompletion?', // i suppose you could change this... + url : 'https://api.foursquare.com/v2/venues/search?', // i suppose you could change this... ll : '37.787920,-122.407458', //default to SF since it's well known v : '20120515', //the date of the foursquare api version limit : 10, //perhaps an option to ignore limits + intent: 'browse', //Looking for geo-specific results + radius: 80000, //default to foursquare max of 80,000 meters (ll and radius are required with 'browse' intent) client_id : "YOUR_FS_CLIENT_ID", //get this from foursquare.com client_secret : "YOUR_FS_CLIENT_SECRET", //same style_results: true //set to false if the way i control the position of results, you can do it yourse @@ -134,6 +136,8 @@ + "&ll=" + opts.ll + "&v=" + opts.v + "&limit=" + opts.limit + + "&intent=" + opts.intent + + "&radius=" + opts.radius + "&client_id=" + opts.client_id + "&client_secret=" + opts.client_secret; @@ -163,17 +167,17 @@ } function buildResultsList() { - minivenues = data.response.minivenues; + minivenues = data.response.venues; if(minivenues && minivenues.length > 0) { results = ""; for (var i = 0; i < minivenues.length; i++) { v = minivenues[i] //TODO this should be customizable, at least for urls - results += "
  • " + v.name + "
  • "; + results += "
  • " + v.name + "- "+ v.location.address +", "+ v.location.city +"
  • "; } } else { - results = ""; + results = ""; } return results; From 41928f4cadd4e1121f5ddb4ae044595853bfdf70 Mon Sep 17 00:00:00 2001 From: Andrew Cafourek Date: Fri, 23 Aug 2013 17:13:09 -0700 Subject: [PATCH 2/3] Global intent fallback - Added fallback search so if no local results are found, the intent is switched to global and all of foursquare is searched. - Commented out some debug lines. - Changed result list to contain data attributes for venue data. --- foursquare_suggest.js | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/foursquare_suggest.js b/foursquare_suggest.js index 592d445..962bfc8 100644 --- a/foursquare_suggest.js +++ b/foursquare_suggest.js @@ -43,7 +43,7 @@ //wait for keyup to get total entered text this.keyup(function(event) { - console.log("key up"); + //("key up"); var code = event.keyCode || event.which; //enter keys and and arrow keys should be caught by keydown @@ -87,7 +87,7 @@ //add global keyup on document this.keydown(function(event) { - console.log("window key up"); + //console.log("window key up"); var code = event.keyCode || event.which; if(code == 13) { @@ -114,7 +114,7 @@ function onEnterKey() { //figure out if anything is selected if(resultsListActive) { - console.log("resulstListActive true: click " + "#" + resultsList.attr("id") + " .selected a"); + //console.log("resulstListActive true: click " + "#" + resultsList.attr("id") + " .selected a"); //there must be a selected item in the list so trigger it's click event window.location = $("#" + resultsList.attr("id") + " .selected a").attr("href"); @@ -129,6 +129,7 @@ resultsList = $("#fs_search_results"); } + var fallback = false; function callFoursquareSuggestion() { //TODO check for options and set up some error checking url = opts.url @@ -140,15 +141,23 @@ + "&radius=" + opts.radius + "&client_id=" + opts.client_id + "&client_secret=" + opts.client_secret; - $.getJSON(url, function() { - console.log("get search results ajax called"); + //console.log("get search results ajax called"); }) .success(function(_data, status, xhr) { - console.log("success"); - console.log(_data); + //console.log("success"); + //console.log(_data); data = _data; - showResults(); + + if((data.response.venues && (data.response.venues.length > 0)) || fallback == true) { + //if we have results OR this is the 2nd attempt, show results + showResults(); + } else { + //if nothing found in local radius, switch intent to global and re-call foursquare + opts.intent = 'global'; + fallback = true; //set global so we dont end up in infinite fail loop + callFoursquareSuggestion(); + } }) .error(function() { //TODO show some kind of error message @@ -174,7 +183,7 @@ for (var i = 0; i < minivenues.length; i++) { v = minivenues[i] //TODO this should be customizable, at least for urls - results += "
  • " + v.name + "- "+ v.location.address +", "+ v.location.city +"
  • "; + results += "
  • " + v.name + "- "+ v.location.city +", "+ v.location.state +"
  • "; } } else { results = ""; @@ -205,14 +214,14 @@ } function downArrowPressed() { - console.log( "down pressed" ); + //console.log( "down pressed" ); if(resultsListActive) { - console.log("active"); + //console.log("active"); //if active and last item in list if(resultsListIndex == liLength - 1) { //do nothing or return to top? //return to top - console.log("reached the end"); + //console.log("reached the end"); setSelected(0); } else { //console.log("down arrow " + (resultsListSelectedIndex + 1)); @@ -221,7 +230,7 @@ } } else { - console.log("not active, activate"); + //console.log("not active, activate"); //activate the list resultsListActive = true; setSelected(0); @@ -229,7 +238,7 @@ } function upArrowPressed() { - console.log( "up pressed" ); + //console.log( "up pressed" ); if(resultsListActive) { //get selected index @@ -250,7 +259,7 @@ } function setSelected(index) { - console.log("set selected: " + index); + //console.log("set selected: " + index); resultsListSelectedIndex = index; $("#" + resultsList.attr("id") + " li").removeClass("selected"); $("#" + resultsList.attr("id") + " li").eq(index).addClass("selected"); From dccdb8440a255f9f62400d95f60d36ddfa4638e3 Mon Sep 17 00:00:00 2001 From: Andrew Cafourek Date: Thu, 9 Jan 2014 11:48:18 -0500 Subject: [PATCH 3/3] Switch back to suggest complete API And other logic/output improvements --- foursquare_suggest.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/foursquare_suggest.js b/foursquare_suggest.js index 962bfc8..adb9191 100644 --- a/foursquare_suggest.js +++ b/foursquare_suggest.js @@ -13,7 +13,7 @@ $.fn.fs_suggest = function(options) { var defaults = { - url : 'https://api.foursquare.com/v2/venues/search?', // i suppose you could change this... + url : 'https://api.foursquare.com/v2/venues/suggestcompletion?', // i suppose you could change this... ll : '37.787920,-122.407458', //default to SF since it's well known v : '20120515', //the date of the foursquare api version limit : 10, //perhaps an option to ignore limits @@ -141,15 +141,15 @@ + "&radius=" + opts.radius + "&client_id=" + opts.client_id + "&client_secret=" + opts.client_secret; - $.getJSON(url, function() { + safe_url = encodeURI(url); + $.getJSON(safe_url, function() { //console.log("get search results ajax called"); }) .success(function(_data, status, xhr) { //console.log("success"); //console.log(_data); data = _data; - - if((data.response.venues && (data.response.venues.length > 0)) || fallback == true) { + if((data.response.minivenues && (data.response.minivenues.length > 0)) || fallback == true) { //if we have results OR this is the 2nd attempt, show results showResults(); } else { @@ -176,14 +176,14 @@ } function buildResultsList() { - minivenues = data.response.venues; - + minivenues = data.response.minivenues; + if(minivenues && minivenues.length > 0) { results = ""; for (var i = 0; i < minivenues.length; i++) { v = minivenues[i] //TODO this should be customizable, at least for urls - results += "
  • " + v.name + "- "+ v.location.city +", "+ v.location.state +"
  • "; + results += "
  • " + v.name + "- "+ v.location.city +", "+ v.location.state +"
  • "; } } else { results = "
    • Couldn't find that venue.
    ";