Geolocation

No one likes to be lost. This helps explain why location services are estimated to be a $10 billion industry in 2015.[17] Geolocation in the Mirror API is the act of learning the location of a Glass device at some latitude/longitude point on planet Earth.

You application can use that information for all sorts of purposes, from getting directions to the cheapest fueling station to having the history of a nearby building projected onto your screen.

Your Glassware receives this information through the Location resource. But before your code can access a device’s location, it must ask the user for permission.

OAuth Location Scope

In the previous couple of chapters we glossed over choosing the correct scope to access resources. Since all of our actions thus far have been on the timeline, the glass.timeline scope had been sufficient. But to access location resources only, without using the timeline, you should request the following scope:

https://www.googleapis.com/auth/glass.location

If you looked at the scopes in our AuthUtils code, we can use both scopes.

chapter-5/src/test/book/glass/auth/AuthUtils.java
 
public​ ​static​ ​final​ ​List​<​String​> SCOPES = ​Arrays​.asList(
 
"https://www.googleapis.com/auth/userinfo.profile"​,
 
"https://www.googleapis.com/auth/glass.timeline"​,
 
"https://www.googleapis.com/auth/glass.location"
 
);

Practically speaking, the glass.timeline scope lets your app access a user’s location. Using both Glass scopes doesn’t hurt if you plan to reference location as well as the timeline. In fact, you should, since during authorization the user can see exactly what you’ll be accessing.

One Location

When your application asks Google to track the location of a Glass device, Mirror provides the positions of the device over time, and assigns an ID to each location. The interesting bits of a Glass location object are the Glass device’s latitude, longitude, and accuracy in meters. Like with all Mirror messages, id and kind will also be populated.

Location is a small but potent object, filled with plenty of information; you can find a complete listing in Appendix 1, HTTP and HTML Resources.

Let’s add a bit of geolocation to Lunch Roulette. To get the device’s most recent location, pass in a special ID latest. If you want and have to get the ID of a certain location object, use that ID string instead.

chapter-5/src/test/book/glass/LunchRoulette.java
 
Location location = mirror.locations().get(​"latest"​).execute();
 
 
double​ latitude = location.getLatitude();
 
double​ longitude = location.getLongitude();

The Java code will generate an HTTP GET request using the ID, and return a populated JavaScript Object Notation (JSON) body. Here’s an example of what the latest location may generate.

 
GET /mirror/v1/locations/latest HTTP/1.1
 
Host: www.googleapis.com
 
Authorization: Bearer e057d4ea363fbab414a874371da253dba3d713bc
 
{
 
"kind"​: ​"mirror#location"​,
 
"id"​: ​"latest"​,
 
"timestamp"​: ​"2013-09-09T22:12:09.745Z"​,
 
"latitude"​: 45.5142245,
 
"longitude"​: -122.6807479,
 
"accuracy"​: 49.0
 
}

You can’t create or update a location yourself (the /locations resource accepts only GET actions, not POST, PUT, or DELETE). However, you don’t need to, since Google populates locations for you.

Many Locations

If you want the history of a Glass device’s movement, you can access a list of previous locations. We won’t access a full list in Lunch Roulette (suggesting a restaurant near yesterday’s position seems somewhat uncongenial), but it’s useful to know how.

In Java, executing location.list returns a LocationsListResponse object. You can get items via the getItems method, which returns an iterable List.

 
locations = service.locations();
 
LocationsListResponse locList = locations.list().execute();
 
for​ ( Location loc : locations.getItems()) {
 
System​.out.println(loc);
 
}

You’ll receive a JSON response with a list of locations (for some reason called items). Each location will contain an id, which your app could store to later retrieve a specific location.

 
GET /mirror/v1/locations HTTP/1.1
 
Host: www.googleapis.com
 
Authorization: Bearer e057d4ea363fbab414a874371da253dba3d713bc
 
{
 
"kind": ​"mirror#locationsList"​,
 
"items": [
 
{
 
"kind": ​"mirror#location"​,
 
"id": ​"latest"​,
 
"timestamp": ​"2013-09-09T22:22:08.640Z"​,
 
"latitude": 45.5142245,
 
"longitude": -122.6807479,
 
"accuracy": 49.0
 
}
 
]
 
}

Glass sends a location update every 10 minutes, so you may notice that location timestamps are about that length apart. The 10-minute window is a value built into the Glass device’s software. Since there’s no guarantee that a user hasn’t modified her Glass in some way, or that she even has consistent Internet access, you can’t ever count on an even 10-minute spread. Google prunes this list every so often, so you shouldn’t have to worry about receiving too many items.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.227.194