LAURENCE MORONEY: Hi, and welcome to Geocache,
where you can walk your way through things that you can
do with the Google Maps APIs.
[MUSIC PLAYING]
I'm Laurence Moroney, and in this episode,
I'm going to talk about the geolocation APIs
and how you can use them to locate devices that may not
have a GPS, such as rapidly emerging breeds of internet
of things devices, or, of course, in places
where GPS may not be available due to tall buildings
or other obstructions.
The geolocation APIs work by accepting
the addresses and signal strengths of cell towers
and Wi-Fi access points near you.
From there, they can calculate a likely latitude and longitude
for your device using a process called trilateration.
It works like this.
When you scan for access points near you,
the APIs in your operating system
will return the unique identifier for those access
points, as well as the signal strength.
So when you have the signal strength and the frequency
at which the access point transmits,
there's a formula called the free-space path loss.
And using it, you can calculate a rough distance
from that access point.
Now of course, this will only give you the distance
from the access point.
But if you have a number of them,
then you can scan multiple points.
And knowing the distance that you are from each,
this will allow you to calculate where an overlap is.
And from there, you can get an estimate for your location.
The geolocation API has the coordinates of many access
points and cell towers.
So if you tell it the address of the points near you
and their signal strength, then it
can use this data to calculate your likely position
using this technique.
So let's take a look at how you do this.
The easiest way is to scan for Wi-Fi signals near you.
Most operating systems support this natively.
In Linux, the command is iw dev wlan0 scan.
Or on Android, you'd use a Wi-Fi manager
that you can get from the system and set its scan results
to a list of scan result types.
You can then access data about signal strength
and the identifier of the access points from there.
Once you have the data, you structure it
into a JSON file like this, listing the access points
you've seen, their signal strength,
signal to noise ratio if available,
and then you simply call the geolocation API,
passing at this data and your API key.
Here's how that would look like with a curl command.
If you're not familiar with the concept of an API key
or how to get one, you can see so in the documentation
at this link.
Now once you pass this to the geolocation API,
you'll get a return value containing a latitude,
longitude, and estimation.
And the estimation is the rough distance
that you are from that latitude and longitude in meters.
This is because it's calculating your location by overlapping
distances using trilateration, giving you
a circle that looks like this.
The accuracy value is the radius of that circle.
And that's how you geolocate using Wi-Fi or cell towers.
This will work in indoor scenarios, or those
where you may not see the GPS satellites-- for example,
in cities with tall buildings.
Particularly exciting is the facility
to geolocate internet of things devices such as this one,
a tiny computer the size of a stick of gum
that has built in Wi-Fi.
Or this one, a robot built on a Raspberry Pi
device that geolocates by sensing Wi-Fi access
points around it.
A great complementary API is the Roads API,
where if you aren't pinpointed but you know that the device is
moving along a road, you can snap the readings to the road.
Here's an example of where I took a drive with a robot
in my car.
The gray line links where the readings were taken,
and geolocation gave me a latitude, longitude,
and estimation.
I passed these to the Roads API, and then I
got the blue line showing that I followed that specific road.
That's the geolocation API.
You can learn more at this link, and if you have any questions,
please leave them in the comments below.
Don't forget to hit that subscribe button
to see more Geocasts.
[MUSIC PLAYING]