[MUSIC PLAYING]
LAWRENCE: Hi, and welcome again to GeoCasts,
where we'll teach you all about using Google Maps to build
better apps.
I'm Lawrence, and in this video I'm
going to talk all about building an interactive place-based
experience using the Places API.
It's a simple game, and you'll be communicating with Alyssa,
this woman who's trapped in a parallel dimension,
and well, I'll just let her explain what you have to do.
ALYSSA: There was an accident.
I'm trapped in an alternate dimension which is literally
right next door to yours.
Parts of my dimension are leaking into yours,
and if you can get to their location,
I can fix it and get myself home.
LAWRENCE: So how does this work as an app?
It's been made user customizable so you can pick any three
places in the world, assign a code to them,
and then when you share that code with your friends,
Alyssa will guide them through those three places
to solve puzzles that will get her home.
The Google Places API supports over 100 million places
around the world so you have lots to choose from.
Here you can see some games that I've
designed stored in Firebase.
So for example, if you were to use the code googkior,
you'd go to these three places to rescue Alyssa.
Or if you were to choose the code draheda,
you'd be guided to these three.
The Places API has a user interface widget
called the PlacePicker, which allows you to pick your desired
places off the map.
In this video, you'll see how to use the PlacePicker to choose
your three places and how to store them in Firebase.
In the next video in this series,
we'll look at how you use the Places API
to detect your current location to see if it matches
the desired place.
As it uses real world places, the Places API
is a terrific way to make it user-friendly, powerful,
and easy to build for.
ALYSSA: It's really nice of you to help me out.
Thanks.
LAWRENCE: You're welcome.
The Places API is designed to make your apps stand out
with detailed information about over 100 million places
around the world, across a wide range of categories,
and it uses the same database as Google Maps.
Not only that, the Places API has place detection,
giving you the ability to tell if your user is
in one of those locations.
ALYSSA: That makes it easier.
I can just tell you to go to a specific location,
like say, the Seattle Space Needle, instead
of dealing with latitude and longitude or addresses.
LAWRENCE: That's right.
And the Places API, remember, can
detect if your user is there.
ALYSSA: Well, of course I'm there, silly.
LAWRENCE: Right, but before we get to detecting the places,
let's look at how we can build an app where
you can configure the places that you want to play in.
We'll use the PlacePicker control to pick three places
and store their place IDs in Firebase
along with a unique code.
You can share that code with your friends
so when they play the game, Alyssa
will tell them to go to those places, like this.
ALYSSA: The first part is here.
Please go and I'll let you know once you're close enough.
LAWRENCE: The PlacePicker control
allows me to give my end users a standard, user-friendly way
of selecting the places that this game will be played in.
So for the game, I decided that it would use three places.
You can see how it works here.
Every time I press a button, I get
the PlacePicker, along with Google Maps,
allowing me to choose a place.
I'll pick the Space Needle for us.
You're still there right, Alyssa?
ALYSSA: Roger that.
LAWRENCE: Good.
So then I can pick some other places.
ALYSSA: And if you really don't like your friends,
you can make them locations that are really far away, right?
LAWRENCE: That's right.
And of course, as you can see here,
these strings are the identifiers
for those places on Google.
Later when we scan for the device location,
we'll find nearby places and we can compare
to see if we're close to these.
So how does this work in code?
First of all, in order to use the PlacePicker,
you need your app to have the access find
location permission, so make sure you have that.
Docs on how to do that are right here.
Next, make sure you have the location
libraries from Play Services included in your builder
gradle.
Now, in an Android activity, and as you
can see mine is called Create Game Activity,
you can use a PlacePicker Intent Builder
to create a PlacePicker intent.
Start this and the picker will run for you.
If you're wondering what that ID parameter is,
this game allows you to define three places,
so it's just an integer for one, two, or three.
Once the user selects a place, OnActivityResult will fire
and it will contain an intent with the returned data.
You can then turn this into a place object with
the PlacePicker .getplacefunction.
The place object exposes the Google identifier
for that location as the place ID, which you can then
read with that method.
It also has the description for the place
which you can access with a place.getname
and cast that into a string.
It's really that simple.
To close the loop, here's how you
can save the three Place IDs to Firebase under a game code.
I simply create a node with the game code
and store these values under that.
Do note that there are restrictions
to saving data from the Places API in your own database.
Before distributing an app that uses the Places API,
be sure to understand the policies.
They're linked right here.
So you've completed the first part of this task.
Well done.
In the next video, we'll take a look
at how to use the Places API to determine if your users are
close enough to the desired place to unlock
the next part of the game.
You can learn more about the Google Places API at this page
and see more GeoCasts videos on the Google Developers channel.
ALYSSA: And don't forget to hit that Subscribe button.
[MUSIC PLAYING]