Get Nearby Places using FourSquare APIs in Windows Phone 8.1

You can use FourSquare’s public APIs to display a list of nearby places in your Windows Phone app. Here’s a quick tip on how to do it.

Step 1: Register your app on FourSquare

You will need to register your app and get a client ID and client secret before you can use any of its APIs. Register here.
https://foursquare.com/developers/apps

Step 2: Define a class Venue to hold the JSON response

The Venue object returned has lots of information about any particular place. You can play around with and put it to many different uses. Here’s the whole RootObject returned by the venue API.

Step 3: Get the device’s Geo Coordinates

Next, use the Geolocator class in Windows Phone 8.1 to get device’s current location.

Step 4: Define a ListView to display nearby places

Define a simple ListView and add a TextBlock in its DataTemplate, binding it to name. To add to the fun, we have added a FourSquare logo in the list’s footer.

Step 5: Make a call to FourSquare’s Venues API

The getNearbyPlaces method makes a call to the venues API of Foursquare to return a JSON response containing a list of nearby places. Finally bind the result to the ListView.

Get GPS coordinates in Windows Phone 8.1 app

If your app needs user’s device location to personalize his experience then the first step would be get its GPS coordinates. We are starting a new category of posts for handling location in windows phone 8. This one simply shows you how to get device’s GPS coordinates.

Step 1. Create a new Windows Phone 8/8.1 XAML project in Visual Studio

Choose Blank App in Store Apps> Windows Phone apps from the installed app templates, name it and create a new project.


Step 2. Add Location capability in Package.AppManifest

In the Package.AppManifest file check the Location capability under Capabilities tab.

Note: If you are developing a Windows Phone Silverlight application, you will have to add ID_CAP_LOCATION capability under Properties > WMAppManifest.xml.


Note: UnauthorizedAccessException occurs if you don’t add Location capability.

Step 3. Create a basic UI for the app

You could add just a button and a text-block to the app. The app executes the code to get the GPS coordinates when the button is clicked and displays it in the text-block.

Now you have two options. One is to get the GPS location once and use it and the other is to continuously track the location to detect any change. In this article we will discuss only single shot tracking. Step 4. Add the code behind to track GPS location

Single-shot tracking

If your app needs to track the location just once, then its best to use single shot tracking. Simply put just get the device’s GPS location and don’t use a position changed event handler with it to update the GPS coordinates whenever the device’s location changes. Here’s the click event handler code to use single shot tracking in your app.

Note:

  • We are using an async button click event so that the app could do other tasks as well and not wait for the result. It does this asynchronously so that the UI thread is not blocked while the location is obtained. You can use the await operator to place code after the asynchronous call that will be executed after the call finishes.
  • DesiredAccuracyInMeters is used to set the desired GPS accuracy level
  • The whole location operation is wrapped in a try block in case any exceptions are thrown.

That’s it. Run the app in the emulator and see it it works for you. In the emulator some random location(of mostly Microsoft in Redmond) is shown. You will get the correct coordinates when you run it on your device.

You can download the full source code of Windows Phone 8.1 XAML project here

Download full source code GPS-Location.zip