Welcome back and this video, we are going to implement a feature for which we will require an account
at Google.
And as you can see, the feature will do the following.
So when I create a new happy place, once I tap on location, it opens up this.
Window here, where I can, for example, search for a specific location, for example, for Berlin
or even a whole address, I can enter a whole address there and then it will populate the field here.
But in order to get there, we, of course, I need to use the Google Maps API and therefore we need
to create an account or have an account at Google.
And you need to log in to console the developers dot Google dot com.
Therefore, you will need a Google account.
Once you are locked in, you can select to control your and then you can read through the terms of service
and agree to them and then click on agree and continue, of course, only if you agree with the terms.
All right, at this point, you can create a new project so you can see there are no projects yet in
this dashboard that I'm using so you can go ahead and create a new project and then you can give it
a project name.
And I'm just going to call this one Happy Places.
One, two, three.
And I'm going to create.
This will then take a little while, and once that is done, you can go ahead and able APIs and services.
So here you can enable a bunch of them.
So you can see there are many different APIs that Google offers.
And the one that I want to use for this particular example is the Places API.
So let's use that click on it.
And then you can press and able hear.
Once you're at the screen, you can go to credentials.
And.
Then you can create a new credential, so your credentials in API and services, that's where you can
create a new API.
So let's create credentials and I want the new APIC.
So it's going to create me one, and this is the one that I can then use.
And I can copy it and I can directly close or restricted, so that's restricted.
And, for example, it requires you to restrict it in order to prevent unauthorized use and, quote,
left.
So if you don't secure your key here and somebody else finds your key, then he can use it and he can
use it for all of his applications and thousands of users might use it.
And then suddenly you're, well, this will cost you.
OK, but usually it can or this for free up to a certain point.
So for the first few tests and so forth, it's going to be for free.
But as soon as you have hundreds of users, then it will be paid.
All right.
So you can restrict your key, for example, for Android apps and you can even restrict that for a specific
app.
So you can go to Android apps here, then add an item and here you enter the package name.
So in my case, the package name, of course, is this one here.
So let's get that.
And pasted in here and then the SHA one fingerprint certificate.
And in order to get the Shah one finger print here, you can, of course, use multiple ways as very
often the case.
So here there are quite some very complicated steps to do that.
But I think the easiest one will probably be, which will be to create a new.
Fragment here, so you fragment and Google Maps fragment.
And I'm just going to create it as is.
And here you can find your SHA one certificate.
So inside of this Google Maps API, XML file down there to scroll down here and underneath it you will
find it.
So I'm not going to share it here, but you can just find it there.
And then once you paste it in here, you can press down.
And then don't forget to press safe.
All right, the next thing that you should do is copy this API key and then go over to your values and
you're going to create a new string.
Which I'm going to call Google on the score maps on the score, API on the squawky, and I'm going to
pass it in here.
Next, we need to add a meta tag in our Android manifest.
So let's go over there.
So you can already see, because I created this new activity or this fragment, it asks me for my Google
Maps key and well, the value is already there.
So metadata, the name and the value here.
And in my case was Google Maps Hibiki.
All right, the next thing we need to do is we need to go to our gradle and here we need to add following
implementation.
So scroll down to the dependencies and then here I'm going to add another one, which is Google that
had not Google's Android Todd libraries that places Kolon, places call to build one dot zero.
Right.
Once you have that, you can use the place as class, which we will require.
So let's go to our ad Happy Place activity.
And here inside of the uncreate, I will create, if not places that is initialised.
Then I want to run some code, so let's make sure that this is important.
And by the way, it will only be visible or available if you built your cradle, so sinked now.
And once it's fully synched, then you can go back.
To add happy place activity, and now you can import it because otherwise it's not available, it doesn't
know this here.
OK, now you can see now knows it now, can import it before it wanted to create an abstract class.
So if that's the case in your case, then you still need to think it.
All right.
Now, if it's not initialized and I want to initialize it and you need to pass in the activity in which
you are currently so.
And happy place activity.
And here you need to pass on the Google Maps API key and you can get that from the resources dot get
string and then our dot strings or string dot.
And in my case was Google.
Maps, API key.
That's how I call it.
All right.
So the next thing I will do is in the UI element that we had, so let's go to our.
UI here, the layout we had in and happy place hours had its text.
Which was for the location, right?
So text input had a text to your outdated location.
So this one here, I don't want this to be flexible.
I want this TextField to be nonfactual.
OK, so pretty much.
Just go to the combat added text for our location ET location and then add to two lines.
Focus, Bo.
Falls and turn focus of all in touch mode also to false.
So now what I want to happen is not to change the added text, but I actually want it to be clickable.
And then once we click it, then it opens up this places activity, which then allows us to enter a
place name.
So now let's go back to our place activity, and here I will add another unclick listener.
So we had this button safe and so forth, all these unclick listeners, and here I'm going to do a location
so I don't click listener.
So it's similar to what we did with a date, right.
So we didn't want to date to be entered like a text or an edit text usually would be.
But we wanted to open up this dialogue, which then allows us to enter a date or select a date.
And here it's the same with the location, only that now it's not going to be a dialogue, but it's
going to be this Google Places API tool that we're using.
So what we're going to need here at the very bottom is a new Constance, because so far we had the consent
for gallery camera image directory, but we don't have one for our place or a complete request code.
So I'm just going to create a new private concert.
Well, which I'm going to call plays or a complete request code.
And this one will be three.
And now when I click on the ET location, so onto this or undecided's text, then I want to run some
code, so I need to have another entry here just underneath safe.
So let's go all the way down here.
So at this point, Hardcourt, I'd thought etty underscore location.
So once we click on the location, we want to execute some code.
And what I'm going to try to do is I'm going to try and get something running here.
This can go wrong and when it does go wrong, I want to catch it with an exception.
So I'm just going to say exception and it would be a generic exception, i.e. that print stack trace.
All right, and now what do I want to try in here?
Well, we need to focus on something that's generally what Google wants us to do, so we when we want
to use this feature, we have to use the following structure.
So I'm just going to paste it in here.
Because there's no use in me entering all of that manually.
So.
First of all, we need to fields, which is a list of places field, I'd place his name Pressfield lat
long, so latitude and longitude and playfield address, and then we can create the intent, which is
the complete intent builder to complete activity mode, full screen.
And then we pass in the fields that we just created.
And then we say, build that, OK?
And then we start this activity for result with the intent that we just prepared and our request code
that we passed in as well, which we call place autocomplete request code.
OK, so it's an activity which pops up on top of our activity, which is this place is functionality
of Google.
And of course, if the request code is our place or a complete request code, we want to do something
on an activity result.
So here on activity result, we had these different Ellsworth's and I need to add another one.
So we had one, for example.
One is the camera and we get something back.
Well, then we want to set the I've place image has this image that was taken with the camera.
And now, of course, we want to do something if the request code is going to be the one from our place
autocomplete.
So when we enter a place and we get something back, well, that's what we want them to display.
So what I'm going to do is I'm going to get this place.
So I'm going to create this new place, which will be the complete.
That get placed from intent.
And the intent will be our data that is passed to us.
So, as you know, we are still in an activity result which gives us request code records, consult
code and data.
So we get those information and we can use them.
So what's left here is to set the location to use a text, placed that address and then, um, latitude
to place that, let alone so latitude and longitude, that latitude and now the same thing with the
longitude and let all in LNG needs to be typed correctly.
So it's this capital L and then here M longitude.
So we called it.
It's going to be placed that let along and here it's going to be the longitude.
OK, so this was a bunch of stuff that is just how it is, that's how Google designed it and we just
have to go with the flow and use this approach as it is.
So this is something that you can, of course, check out in the documentation and get inspired there.
But basically, this is what you need to build in order to get the functionality that you've seen earlier
on.
There's one thing that is quite important to get this running, though, and that is that you need to
have billing information, had it here.
So at this point, you need to have a billing account and you can link one here, or you can manage
your billing accounts and you can just set one up here.
I'm not going to do that in this video, but you just have to go through the process to add a billing
account here because otherwise it will not work.
So otherwise this feature will not work in general.
Google API will not work even though they give you a lot of it for free.
So you can really test around with it for free.
But as soon as it comes to having many users, then of course you will have to pay.
All right.
So that is something that you need to look into and you can learn more about it here is you get a free
credit in this case or three hundred dollars.
So you can really explore the cloud products and test around quite a bit.
And it's going to be free for testing most of the time.
But really, as soon as it comes to bigger projects which really work well and make money, then it's
a different story.
OK, so then of course some things can go wrong.
And when things go wrong with this autocomplete, load a screen or load search results, which we are
going to test in a second, then you need to look into those potential savings.
So here Nobley account associated to the project.
If you are still using the old places SDK, you could run into issues, which we don't then if the quarter
limit is reached, which in our case isn't because we haven't even started yet, and if the the app
is not properly restricted.
So that is also something that you can define in the API key settings.
OK, and at that point coming back to the project, we can start it.
And by the way, I want to give you a side note here.
I run into very weird issues here.
I had very weird bugs where for me this feature didn't work.
And by the way, I'm not using the free API key anymore, so I'm using another one where I actually
have something assigned to it or where I actually had a billing account connected to it.
So I run into the issue here when I press the button and went to location and when I entered something,
it wouldn't work, so it just wouldn't work.
Now concede works.
I can just select a location, for example, Berlin, and well, edit this location for me and you can
even go further.
So you can really add like the whole address actually.
So I don't know, like something like a furniture store in cologne.
That's why I used to live.
So you could even go that far.
All right.
And by the way, the issue that I had was something that didn't make any sense.
So I copied it and sent it over to my friend that my project sent over to my friend who tested the project
as well.
And for him, it worked.
For me, it didn't work.
All right.
So I tested this project for me.
The project worked.
And for, uh, for me, this project still didn't work.
So my project still didn't work.
So sometimes it's super weird.
And what you can do in these cases is you can close your project.
And then inside of the project itself, you can delete the great idea and build folders, but only once
you close the project and then inside of the app.
Also this build folder.
So delete those three folders here.
So these three and then in the app build as well.
So delete them and then restart your application and then we'll have to do the whole gradle set up and
everything once again.
And then it worked for me.
So it was not the code, it was not the API key was.
None of that was for whatever reason we're back and that is something that just happens.
And then you just have to go through it.
And I hope it works for you directly.
But if it doesn't, then try these things.
Google around a little bit.
And if you still can't figure it out, then of course, write a message and the question and answer
session.
All right.
So I hope really that has worked for you.
And then, yeah, I think now it's time to go to the next video where we are going to implement the
Google Maps features, because so far we used to place this feature, which is cool, so we can get
locations, which is great.
But we also want to be able to select a spot or set our current location, for example, and also see
the location on the map and those kind of things.
So that's what we're going to look at in the next video.
So see you there.