Click here to Skip to main content
15,861,168 members
Articles / Programming Languages / C#

Exploring GoogleGears Wi-Fi Geo Locator Secrets

Rate me:
Please Sign up or sign in to vote.
4.93/5 (42 votes)
14 Apr 2010CPOL5 min read 200.7K   6.6K   83   38
This is a C# application that uses the Google Location service to retrieve location information using Wi-Fi towers.

Story

I knew about Google Latitude from a friend when I bought my HTC mobile. I used it a few times before I updated my ROM to a cooked ROM that has no Google Latitude app installed. The story begins when I noticed by chance that my Google Latitude gadget on iGoogle reflected my location although I had not accessed Google Latitude from my mobile recently. I Googled for geo features of Google Gears and discovered that they are able to give you your location using Wi-Fi towers only. What follows is a discussion about Google Gears geo features and a proposal of how it might work. I developed a tool as a proof-of-concept of how can we use Wi-Fi geo location directly using a simple C# application.

Introduction

The article is divided into two sections. The first section is an investigation and ideas about how Google can get geo location using only local Wi-Fi! The second part is the coding part, in which we make an application that uses Google services to get geo location.

WifiLocator.PNG

Background

First, try to connect your computer to a wired network - no Wi-Fi - and then try to find your location using Google Latitude. Google will give you "Unknown Location" as it will not be able to locate your location. Switch the very same computer to a Wi-Fi connection and use Google Latitude again. Now the result will be completely different - Google can give you accurate coordinates of your location. That means the location cannot be determined by the IP address only. Using IP, you can know the country, city, and regions in case you know from ISPs how they distribute their IPs geographically.

The second thing I heard about is that BBC has a service that gives you weather information based on your location that it gets from Google Gear. I decided to monitor HTTP traffic using a tool such as Fiddler. I was looking for:

  1. What data is retrieved from my PC in order to locate my location?
  2. How does BBC communicate with the Google location service via my PC?

The BBC weather site downloads JavaScript to my PC that locally calls Google Gears, and then Google calculates my location and sends it to the BBC website. The following figure shows this information. As we can see, Google Gears collects all Wi-Fi towers that can be detected from my PC in the following form: { mac_address , signal_strength, SSID}.

HTTPView.PNG

This data is sent in JSON format to www.google.com/loc/json and Google replies with my latitude, longitude, and even my own address. The above information that is sent to Google is good enough to roughly locate your position relative to these Wi-Fi towers, but to locate the absolute location, you need to know the absolute location of these Wi-Fi towers!! Again, it is not clear to me how Google knows such information. I made some more investigation and found that in case GSM data is available, this data is also sent together with the Wi-Fi information.

How does Google Gears Geo Location work?

From here, I will try to describe, based on the above info, how Google might use this information to give location information. In the case of GPS, data sent to Google contains:

  1. GPS data
  2. GSM towers
  3. Wi-Fi towers

In the case of a mobile that supports a GPS, it is enough to determine the GSM towers to have accurate info about location. This info is sent to Google as part of the request. But as we can see, this information already contains the geo location, so Google might do the following:

  1. Calculate you location [latitude and longitude].
  2. Use Wi-Fi data to calculate your relative position to each Wi-Fi.
  3. Calculate the absolute position of each Wi-Fi tower based on a and b:
    1. Mobile absolute location is already known.
    2. Wi-Fi relative location from the mobile is known.

StationsWifi.png

Now, Google stores the mac_address and the SSID together with the absolute location in a geo database that is kept updated when possible.

In case you open your laptop and connect to Google Latitude, Google can determine your absolute location using the Wi-Fi towers only because it already knows, from other queries from you and different persons, the absolute location of the Wi-Fi towers. As we can see in the following figure, the blue person sends GPS, GSM towers info, and Wi-Fi towers info. Google calculates the Wi-Fi towers info. Now the green person can know his location by querying Google with Wi-Fi towers only.

This is not that easy. We need to think of many challenges such as similar Wi-Fi names, changing mac-addresses, Wi-Fi turning on/off etc.

Let's Make Our Application

The desktop application that I made here is a POC, and is pretty simple. It works in two steps:

  1. Get Wi-Fi data from PC.
  2. Send this info to Google and wait for response.

The first part is possible using a useful library to access Wi-Fi information from C#.

Get the Wi-Fi adapter:

C#
Json = @"{ ""host"" : ""Test"", 
                ""radio_type"" : ""unknown"", 
                ""request_address"" : true, 
                ""version"" : ""1.1.0"", 
                ""wifi_towers"" : [ ";
lstWifi.Items.Clear();

WlanClient wLanClient = new NativeWifi.WlanClient(); // Get Wifi Interface
if (wLanClient.Interfaces.Length == 0)
{
    MessageBox.Show("No Wifi Interfaces found.");
    return;
}
Wlan.WlanBssEntry[] lstWlanBss = wLanClient.Interfaces[0].GetNetworkBssList(); 
if (lstWlanBss == null)
{
    MessageBox.Show("No networks has been detected.");
    return;
}

Enumerate the available Wi-Fi towers and concatenate the JSON string.

C#
System.Text.StringBuilder SB = new StringBuilder();
foreach (var oWlan in lstWlanBss )
{
    ListViewItem lstItem = lstWifi.Items.Add(
       System.Text.Encoding.UTF8.GetString(oWlan.dot11Ssid.SSID));

    lstItem.SubItems.Add(CalculateSignalQuality(oWlan.linkQuality).ToString());
    string MAC = ConvertToMAC(oWlan.dot11Bssid);
    lstItem.SubItems.Add(MAC);
    SB.Append(@"{""mac_address"" :""");
    SB.Append(MAC);
    SB.Append(@"""");
    SB.Append(@", ""signal_strength"" :");
    SB.Append(CalculateSignalQuality(oWlan.linkQuality).ToString());
    SB.Append(@", ""ssid"" : """);
    SB.Append(System.Text.Encoding.UTF8.GetString(oWlan.dot11Ssid.SSID, 0, 
                                                 (int)oWlan.dot11Ssid.SSIDLength));
    SB.Append(@""" },");
}
Json += SB.ToString().Substring(0, SB.Length - 1); // copy all except last ","
Json += "]}";

textSent.Text = Json;

The second part is to form an HTTP request and parse the response.

C#
HttpWebRequest request = 
   (HttpWebRequest)HttpWebRequest.Create(@"http://www.google.com/loc/json");
request.ContentType = "application/json; charset=utf-8";
request.Accept = "application/json, text/javascript, */*";
request.Method = "POST";

using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
    writer.Write(textSent.Text);
}
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
string json = "";

using (StreamReader reader = new StreamReader(stream))
{
    while (!reader.EndOfStream)
    {
        json += reader.ReadLine();
    }
}

txtResponse.Text = json;
ParseLocation(json);

Points of Interest

If the Wi-Fi section is not working with you if you don't have Wi-Fi, you can still use the application by editing the request text to retrieve different locations.

Disclaimer

The above Google technique is only a guess by me based on simple evidence and logic thinking. This document describes the analytical thinking and how I deduced this technique that could be partially or totally untrue.

Useful Links

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiontime of measuring Pin
Member 127739543-Oct-16 10:21
Member 127739543-Oct-16 10:21 
Bugbug fixed Pin
gfazzola5-Dec-15 2:53
gfazzola5-Dec-15 2:53 
QuestionTnanks Pin
gfazzola5-Dec-15 2:48
gfazzola5-Dec-15 2:48 
QuestionUnfortunately, it seems as if google doesn't return any requests anymore Pin
FredWah29-Nov-12 11:11
FredWah29-Nov-12 11:11 
QuestionRe: Unfortunately, it seems as if google doesn't return any requests anymore Pin
Member 964160129-Nov-12 17:43
Member 964160129-Nov-12 17:43 
AnswerRe: Unfortunately, it seems as if google doesn't return any requests anymore Pin
Mohammad Said Hefny29-Nov-12 18:14
Mohammad Said Hefny29-Nov-12 18:14 
GeneralRe: Unfortunately, it seems as if google doesn't return any requests anymore Pin
Member 964160130-Nov-12 0:23
Member 964160130-Nov-12 0:23 
GeneralRe: Unfortunately, it seems as if google doesn't return any requests anymore Pin
FredWah30-Nov-12 2:04
FredWah30-Nov-12 2:04 
QuestionNot works, please update application Pin
rafaelr.faria7-Nov-12 1:00
rafaelr.faria7-Nov-12 1:00 
GeneralMy vote of 5 Pin
twphoenix17-Oct-12 17:34
twphoenix17-Oct-12 17:34 
QuestionCan Wifi Signal be used to find distance of its adhoc clients? Pin
GPUToaster™14-Sep-12 0:50
GPUToaster™14-Sep-12 0:50 
AnswerRe: Can Wifi Signal be used to find distance of its adhoc clients? Pin
Mohammad Said Hefny29-Nov-12 18:03
Mohammad Said Hefny29-Nov-12 18:03 
GeneralRe: Can Wifi Signal be used to find distance of its adhoc clients? Pin
GPUToaster™4-Dec-12 21:44
GPUToaster™4-Dec-12 21:44 
Questionhello Pin
ram vilas pawar9-Dec-11 7:19
ram vilas pawar9-Dec-11 7:19 
GeneralMy vote of 5 Pin
Sergio Andrés Gutiérrez Rojas26-Sep-11 18:27
Sergio Andrés Gutiérrez Rojas26-Sep-11 18:27 
GeneralWINXP sp3 not working... Pin
nacl1-May-11 9:17
nacl1-May-11 9:17 
GeneralRe: WINXP sp3 not working... Pin
Mohammad Said Hefny12-Apr-12 0:03
Mohammad Said Hefny12-Apr-12 0:03 
GeneralUrgent: ERROR:The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?) Pin
noureddine6-Apr-11 0:04
noureddine6-Apr-11 0:04 
GeneralRe: Urgent: ERROR:The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?) Pin
Mohammad.Hefny29-Jul-12 15:08
Mohammad.Hefny29-Jul-12 15:08 
GeneralGoogle Andoid shows how can wifi together with GPS determine places...notice the wifi cancelled cell in the topic Pin
Mohammad Said Hefny9-Dec-10 7:58
Mohammad Said Hefny9-Dec-10 7:58 
Generalsome thoughts Pin
Spypan20-Aug-10 23:22
Spypan20-Aug-10 23:22 
GeneralRe: some thoughts Pin
Mohammad Said Hefny29-Nov-12 18:04
Mohammad Said Hefny29-Nov-12 18:04 
GeneralVery informative article Pin
Jitendra Zaa14-Apr-10 22:34
Jitendra Zaa14-Apr-10 22:34 
GeneralExcellent! I just summary the useful links for WIFI Geolocation. [modified] Pin
dancefire22-Mar-10 23:01
dancefire22-Mar-10 23:01 
GeneralRe: Excellent! I just summary the useful links for WIFI Geolocation. Pin
Mohammad Said Hefny31-Mar-10 6:31
Mohammad Said Hefny31-Mar-10 6:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.