LAURENCE MORONEY: Hi, and welcome again
to Geocasts, where we'll teach you
all about using Google Maps to build better apps.
I'm Laurence, and in this video, I'm
going to talk all about building an interactive experience using
the Places API.
It's a simple game where you can define three real-world places
and then have your users go to them as part
of the overall narrative of saving Alyssa here
from being trapped in a parallel dimension.
In part one of this tutorial, we identified and stored
three places using the Places API, Firebase,
and some unique game codes.
So the next thing to talk about is
how to use the Places API to see if your user is
at the right place, so that they can unlock
the next part of the story.
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.
We used this information in part one of the tutorial.
But not only that, the Places API
has location detection, giving you
the ability to tell if you are in the right place.
ALYSSA: That makes it easier.
I can just tell you to go to a specific place, like, say,
the Seattle Space Needle, instead
of dealing with latitude and longitude or addresses.
LAURENCE MORONEY: That's right, and the Places API can
detect if the user is there.
ALYSSA: Well, of course I'm there, silly!
LAURENCE MORONEY: So let's take a look
at how you would build the app to scan and detect
where you are.
ALYSSA: OK, I see you are scanning
for the bridge between our dimensions,
but you aren't close to it yet.
LAURENCE MORONEY: In the previous video,
we saw how the user can pick three real-world places
and save them to a Firebase database using a code
to define them.
So if you want to play the game, then you
start it with that code, and you'll
be guided through those three places
as part of the narrative.
But of course, the game needs to know
if you are at that place in order to unlock
the next part of the story.
So let's take a look at the code to achieve this.
It uses the Place Detection API's GetCurrentPlace call
to find the current location.
This requires the Access Find Location permission
to be set in your app.
Remember, you can learn all about permissions at this link.
Do note that this is subject to limits.
So make sure you don't call it too often.
You can learn more about the usage limits here.
So, when building an experience like this one,
instead of constantly polling the device location
and calling the Places API to get place information,
it's probably better for you to do it on demand.
I.e., in this case Alyssa told me to go to a specific place.
So when I think I'm nearby, I can ping her
and she can tell me if I'm close enough or not.
The call returns a place likelihood buffer in a pending
result. A place likelihood buffer
is a list of places near your current location
and the likelihood that you are at that place.
So for example, in a dense city area,
there could be two places next door to each other
which are recognized by the Places API.
And if you're in between the two,
the Places API will return both of them,
as well as anything else nearby.
This will come with a likelihood of which one you're in.
So all you have to do is look at this list
and see if the place you are looking for is on it,
and if the likelihood that you were there
is above a reasonable threshold-- say, 60%, or 0.6
in this case.
And then you could go on to the next part of the game.
It's really that simple.
ALYSSA: If it were that simple, I
wouldn't have been trapped in this parallel dimension.
I guess I should have used the Places API instead of the flux
capacitor.
LAURENCE MORONEY: Good idea.
Thanks for watching this Geocast,
and if you want to learn more about Google Maps,
check out the Google Developers site,
and don't forget to hit that Subscribe button.