[MUSIC PLAYING]
DERECK BRIDIE: Hello, and welcome to this workshop
on building an augmented reality app using the newly announced
ARCore geospatial API.
My name is Dereck Bridie, and I'm a developer advocate
working on ARCore.
Today we'll learn how to get started with the geospatial API
by building a simple location-based
augmented reality experience.
The ARCore geospatial API unlocks new augmented reality
potential by allowing you to accurately pinpoint
a user's location and heading in an ARCore session
by using Street View images combined with GPS signal data.
It also lets you place anchors that
are tied to a physical location using GPS coordinates.
These same capabilities already power
the AR Live View in Google Maps and the AR Places filter
in Google Lens.
Today we'll use these very same features
to get to know the geospatial API.
In this workshop, we'll make an app
that displays geospatial data obtained from ARCore
on a Google map, which is continually updated
as the user moves and turns.
After that we'll listen to tap events on the map
and use that interaction to place
AR content at the real-world location designated
by the user.
If at any point you feel you need some extra time,
please feel free to pause the video.
And if you want to read along or copy some code,
please visit the CodeLab on the AR developer's website.
During the workshop I'll be using recording
of my device's screen.
But I encourage you to try the app outside.
This improves the experience with a better GPS
signal and Street View imagery.
To participate in this workshop, you'll
need a working install of Android Studio
and a geospatial-supported device.
Please consult the ARCore supported devices page
if you aren't sure if your device is
supported by geospatial.
And with all of that out of the way, let's get started.
Let's get started with Android Studio.
From the Welcome to Android Studio tab,
lets click Get from VCS.
We'll be using a pre-made project
to get us started with the geospatial API.
It's the codelab-geospatial repository located
in the google-ar organization.
If at any point you get stuck, make
sure to check out the Git branches, which will help you
start from a new checkpoint.
Click Clone, and wait for the project to open.
Here you'll be able to see the hello-geo project.
Let's take a quick look and ensure that everything's
in the right place.
You should be able to see the HelloGeoActivity
and the HelloGeoRenderer.
Open up the HelloGeoActivity, and scroll down
to the configureSession where it has the TODO.
You'll be able to see that we're going
to configure ARCore to use the geospatial mode enabled.
But first, we're going to have to start with the Google Cloud
platform console.
We're going to need to enable the Google Cloud
APIs that we're going to be using in this project.
So let's head over to the Google Cloud console.
Here I've already created a project.
But if you still need to create a project,
please feel free to pause the video and create a new project.
Let's set up GCP by enabling the APIs that we'll need.
Open the Hamburger menu.
And in APIs & Services, we'll use Library.
Here we'll be on enabling the ARCore API.
So use the Search menu to search for "arcore."
Click the ARCore API, and then click Enable.
We'll be following the same steps
for the Maps SDK for Android.
So once this is done enabling, let's continue on.
Back in Library, we'll be searching
for the Maps SDK for Android.
Make sure you click the Android one as well.
And then we'll be enabling this API as well.
Now we'll be enabling the credentials
that we'll be using for our app to work with Google Cloud.
In APIs & Services, go to Credentials.
Create Credentials here on the top, and click API key.
This is the key that you'll be using in your Android app,
so make sure you copy it out.
Return to Android Studio, and let's open the AndroidManifest.
Near the bottom, you'll see the configuration spots
where the API key is used.
And you'll be replacing the values here with your own API
key obtained from Google Cloud.
Let's select the AR API key and put in our API key here.
And we'll do the same for the geo key.
The first API key authenticates your app
with the ARCore geospatial API.
And the second API is used with Google Maps.
After this is all done, let's close up our AndroidManifest
and start coding.
So in the HelloGeoActivity, we're
going to work on our first TODO, which
is to configure ARCore to use the geospatialMode.ENABLED.
We're going to be configuring our session.
So use session.configure.
And we'll be using the existing configuration on the session.
And we'll be modifying this config
to use the geospatialMode.
And we're going to set it to ENABLED.
And that's all we needed to enable the geospatialMode.
Let's test out that our app works with the given API keys.
So hit Run.
And wait for the app to come up on your device.
If all has gone well, you should be
able to see a map on the bottom half of the screen and the AR
pass through a camera on the top.
If you don't see a working map on the bottom,
please make sure that you've filled in the correct API
keys in the correct location and that your Google console
settings are all good.
OK, let's start with the first part of the API.
Let's head over to the HelloGeoRenderer
and find this first TODO, which is "Obtain Geospatial
information and display it on the map."
The first thing we're going to do
is put the user's location on the map using a marker.
We're going to be using the data from the geospatial API
here so that it's enhanced with Street View information.
First thing we're going to do is obtain the earth.
The earth is the entry point to the geospatial APIs
and is what you're going to be using in this CodeLab.
We're going to ensure that the earth has the tracking state
TRACKING, which means that it's ready to be able to be used.
In this block, you're able to query the earth for all
the information that it has.
One thing we're going to query is the cameraGeospatialPose.
And we're going to use earth to request this.
Let's take a look at what the cameraGeospatialPose
has to offer.
Here you'll be able to see information
like the current longitude, the latitude, the altitude,
and the heading for the device in your hands.
And also, the API provides accuracy information,
which is the confidence that ARCore
has that the real value is within the reported value.
Let's use the cameraGeospatialPose
to position the marker on the map.
We're going to head into the activities view.
And we're going to inspect the map view
and update the map position.
We're going to be using the latitude value here
and use the cameraGeospatialPose,
the longitude as well.
And we're going to be using the heading information.
Finally, we're going to update some debug information
so that you can see the values change in real time.
We're going to be updating the status
text with the current earth and the cameraGeospatialPose.
After you're done adding this to your file,
let's hit Run and inspect the app again.
As the app runs and you hold up your device,
you should be able to see that the earth state turns
to enabled and that the tracking state turns to TRACKING.
As you move around and rotate your device,
you should be able to see that the latitude and longitude
and altitude update.
Make sure to rotate your device around
to see that the heading values change.
Let's move on to the next step.
We'll be placing an anchor using the map on the bottom.
Let's scroll down a little bit to the onMapClick function
where it has the next TODO, "place an anchor at the given
position."
OnMapClick is called when the user clicks on the Google map
on the bottom.
And the first parameter is the latitude and longitude
that was clicked.
We're going to start off by obtaining the session earth one
more time.
And of course, we're going to check if the tracking
state is TRACKING.
And if it's not, we're going to quit early.
Now that we're certain that the earth is in the correct state,
we're going to be modifying the earthAnchor variable.
So if the earthAnchor exists already,
we're going to detach it from the scene.
And then we're going to modify the earthAnchor.
We're going to create a new anchor.
And we'll be choosing the function
that has the latitude, longitude, and altitude values.
We'll be obtaining the latitude from the function
parameter and the longitude from the same place.
And then we'll determine the altitude.
So the Google Maps API does not give you the altitude values.
So we're going to use the current camera position
and maybe subtract a little bit so that you
can look at the values nicely.
So again, we're going to query the cameraGeospatialPose
and look for the altitude.
And let's subtract a little value off there,
let's say 1.3 meters.
Then the next values are the rotation values.
The rotation values are expressed in the quaternion.
That's a little complicated and math-y
to get into in this CodeLab.
So let's use the unit quaternion in this CodeLab, which
is 0, 0, 0, and 1.
And this will determine the rotation of your newly created
earthAnchor.
After that, we're going to be updating the location
of the earthMarker on the map.
So let's head into the activity view and modify its values.
We're going to be setting the new position
to be the latLng that was clicked
and also ensure that it's visible by setting
isVisible is true.
Once you're done here, click Run.
When the app appears on your device,
you should be able to see the map again.
But this time, let's click on a location in the map near you
and inspect the anchor that appears.
As long as the accuracy is doing well,
the anchor should stay stable in the environment,
and you should be able to look at it.
If you click out a new location, you'll
be able to see that the anchor moves.
And you'll be able to follow it with your device.
That about wraps up this workshop
on building an AR app using the geospatial API.
I encourage you to try the app outside and see
how the location confidence changes as you scan
the environment around you.
I'm so excited to see what new location-based AR experiences
you'll build with these new capabilities.
Take care, and I'll see you in the next one.
[MUSIC PLAYING]