Saturday, July 12, 2014

Find Places Using Facebook

In this post I want to show, how to perform search queries using the Facebook API, especally for locactions (places). The principle is the same as for the general reading of data from Facebook, only we here use the search function. Because of this reason we also need, as opposed to the sample program of the previous post, a user acces token. How to get one, you can read in the linked post.
When searching for places there are then multiple possibilities, I will present 2 here: Search by coordinates or by keywords.
To look for places next to specific coordinates, the following query is needed: &type=place&center=LATITUDE,LONGITUDE &distance=DISTANCE"

Here the placeholders in capital letters have to be replaced by the correct values. Latitude and longitude have to be submitted in decimal notation (with a decimal dot), positive values represent North and East, negative ones South and West. So is for example (37° 27′ N, 122° 11′ W) = 37.27, -122.11.
Via the parameter distance the distance from the coordinates in meters can be expressed, up to which locations shall be checked.

To search for keywords we use &type=place &q=KEYWORDS

Via the parameter q keywords can be submitted which describe the place, for example parts of the name, the city in which it is located etc.

Of course both queries can be executed as always in the browser too, or we use a HTTP request in C#, what we want to do here.
Before showing the code we first have to talk about the output format.
If you perform the query in the browser, you get the following result:

   "data": [
         "category": "Club",
         "category_list": [
               "id": "354275401259620",
               "name": "Nightlife"
         "location": {
            "street": "Liebigstra\u00dfe 19",
            "city": "Aachen",
            "state": "",
            "country": "Germany",
            "zip": "52070",
            "latitude": 50.789739317735,
            "longitude": 6.1094749147791
         "name": "",
         "id": "106940009344240"

The result is given in the JSON format, which stands for JavaScript Object Notation and is an easy textformat for data exchange.
To be able to interpret the format in C# we use Json.Net, which can be downloaded here. Afterwards we have to add a reference in our projet to the Newtonsoft.Json.dll, which is located in the BIN folder of the installation and then the folder of the corresponding .Net version.
The following program looks for places close to the given coordinates and prints ID, name etc in a textbox:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;

namespace FacebookSearch
    public partial class Form1 : Form
        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            string RequestURL;
            RequestURL = "" +

            // using Keywords:
            // RequestURL = "";

            WebRequest GetRequest;
            GetRequest = WebRequest.Create(RequestURL);

            Stream ResponseStream;
            ResponseStream = GetRequest.GetResponse().GetResponseStream();
            StreamReader ResponseReader = new StreamReader(ResponseStream);

            string TempLine = ResponseReader.ReadLine();

            JObject FoundPlaces = JObject.Parse(TempLine);

            foreach (var x in FoundPlaces["data"].Children())
                string CategoryName = "";

                textBox1.Text += "Name:" + x["name"].ToString() + "   ID: " + x["id"] +  "   City: " + x["location"]["city"] + "    Street: " + x["location"]["street"] + Environment.NewLine;


No comments:

Post a Comment