Getting Venues with FourSquare API




This example will demonstrate how
to fetch certain venues with FourSquare API V2 using C#.




The following is a link to a live demo: http://foursquare.azurewebsites.net/





Step 1 - Client Creation:


In this example, I’m using a client library called SharpSquare and the following is the SharpSquare client creation.



var assembly = Assembly.GetExecutingAssembly();
//todo: use XML instead
var resourceName = "client_secrets.txt";

string clientId = null;
string clientSecret = null;
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
using (StreamReader reader = new StreamReader(stream))
{
clientId = reader.ReadLine();
clientSecret = reader.ReadLine();
}
}
//Do not try to create the client without proper credentials
if(!(string.IsNullOrEmpty(ClientId) || string.IsNullOrEmpty(ClientSecret)))
SharpSquare = new SharpSquare(ClientId, ClientSecret);





Setp 2 - Get Venues:


The following method gets venues by category within a
certain radius given a postal code using the
FourSquare API V2. The search API is being used here to discover 'any' venue within a category. If you are looking for 'top' (or recommended) venues, explore will probably be a better option.



/// <summary>
/// This method gets the Venues for the provided postal code within the specified radius and category.
/// The default value for intent (checkin) is being used.
/// Upper bound for radius and limit is not checked as it controlled by the API
/// </summary>
/// <param name="postalCode"> The postal code that the radius will be measured from </param>
/// <param name="category"> The category. The default is Food </param>
/// <param name="radius"> The radius in meters. Up to 100,000 with a default of 1000</param>
/// <param name="limit"> Number of records to return. Up to 50 with a default of 30</param>
/// <returns>In case of success, a list of venues. Otherwise null</returns>
/// <exception cref="ArgumentException">This method can throw an ArgumentException upon invalid input or propagating exception from SharpSquare</exception>
public List<FourSquareEntityItems<Venue>> getVenues(string postalCode, string category = "Food", int radius = 1000, int limit = 30)
{
// basic input validation
if (string.IsNullOrEmpty(postalCode) || string.IsNullOrEmpty(category) ||
radius < 1 || limit < 1)
{
log.Error("Invalid input");
throw new ArgumentException(FourSquare.Data.Properties.Resources.Invalid_Input_en);
}

// let's build the query
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("near", postalCode);
parameters.Add("radius", radius.ToString());
parameters.Add("limit", limit.ToString());

// The same can be achieved by querying for all the categories:
// List<Category> c = sharpSquare.GetVenueCategories();
// taking all the sub-categories of 'category' and filtering results by that
parameters.Add("cat", category);
log.Info(string.Format("FourSquare query with: Postal Code [{0}], Radius [{1}], Limit [{2}], Category [{3}]", 
postalCode, radius.ToString(), limit.ToString(), category));
// Execute the query
return SharpSquare.SearchVenues(parameters);
}




Comments:



  1. I used an external file to keep
    the client secrets (you can get a client credentials by creating new app
    here). This will allow
    you to modify the secret without rebuilding the project (keep the file outside
    the assembly).

  2. This example demonstrated retrieval of venues. Feel free to manipulate this sample code to get other information form FourSquare API.

  3. Like in any other project we (software
    developers) have, we try to follow the basic principal: “do not reinvent the
    wheel”. For example, I was involved in a project that needed integration with
    Google for authentication and offline
    access
    to the Drive
    . Following this important principal, I used Googles official
    client library for dot Net. Unfortunately,
    I couldn't find an official dot Net library written by FourSquare. Luckily, an open source one is
    available on GitHub called
    SharpSquare (the same one that is mentioned above).



That's it. Good luck!











Comments