Hi everyone.
So now it's actually at the our own logic to this enclave Smurfit.
First of all it's actually create a body of the.
And the name of this variable is going to be actually the address
value by assignment operator.
EDT address that get text get text that to a strange semicolon.
Right.
So we are actually getting the value of these edits.
This edit the address and we are assigning the value to this address value.
Let's create a variable of type Boolean here.
So boolean and the name of this variable is going to be is geocoding.
So first let me actually tell you what is a joke coding coding.
Right.
So let me actually uncapitalized this.
See assignment operator.
True.
So what is geocoding.
Geocoding is the process of converting the latitude and longitude coordinates to an actual address value
that is actually human readable right.
So let me repeat again the geocoding process is going to convert the latitude and longitude coordinates
to our human readable text.
So that's called geocoding.
And we created a boolean value of N here.
The name is is geocoding.
And initially I was saying the value true to this value.
So now we need a variable in order to hold that destination location address.
So let's scroll to the top here.
This is going to be an instance variable.
So let's actually declare it inside the main activity class so here below this declaration of this button
I can actually say private's.
And by the way it's better to actually declare your value but as these user interface components are
actually as private so that it is only private to the main activity class so private.
And the name of this first you need to specify the data type of this value which is going to be streamed
the name of this rally is going to be destination destination location address.
And let's actually initially assigned an empty string value to this variable.
So let's scroll down here.
And here I can actually create an if statement in say this unclick in effect and after this line of
code that actually creates this is geocoding value.
I can actually say if address so address value I am referring to this added value.
That's actually the way to say it is not equal to.
So I need to pass I need to put this ban here.
This is called bank right.
Not not.
Operator dot equals destination location address so why am I checking for this condition here.
I am actually taking that if the old old adage that the user has entered is actually equal to the destination
location address that is already actually entered.
So let me ask you to show you the emulator.
So here it is our emulator right.
And if the is it actually enters an address here and if the user actually clicks on this get the data
button the user is going to actually get some data.
Ok for example the distance between the current location and the destination location and the actually
remained time in order to get there.
Actually the time left in order to get to that destination location if the user actually wants to click
on this button again this statement is not going to be executed because the address value located the
value that is inside of this text is actually completely the same as the value that is assigned to this
destination location address.
So we're going to actually assign a value to the initial location address inside the if statement.
So that's what I am actually taking for this condition.
I don't want this actually process to be executed actually two times.
I just want the new address to be executed only once.
And if the user gets the address gets these that actually gets the data.
And that's going to be actually enough for that specific address.
OK.
So I don't want the address value to be equal to the destination location an address.
So now not is that the address value is the value of this.
It is the address.
OK.
So let's actually go inside the standard studio.
Let's open and close this if statement by using this curly braces.
So let's see what's what you're going to do here.
Inside each statement.
First of all are you going to actually sign that this nation location address to the address value right.
Destination location address assignment operator at this value so you can see that we are assigning
this destination location at least that is the value if it is not equal here.
All the industry is going to execute the lines of code inside this if statement.
Right.
So this is the first thing that you want to do.
And now you want to actually create an object of type geocoder in order to convert the latitude and
longitude coordinates to a human readable address OK to a human readable actually de-value so that the
user can see what's going on here.
For example the amount of time the distance so I can actually say geocoder geocoder is a class inside
that.
And with that location package geocoder and the name is going to be geocoder the name of the variable
assignment operator new.
Right.
Geocoder semicon.
So here we actually created this object of type geocoder and we assigned to this object to this valuable
type geocoder.
So now this variable halts this object right.
And as you can see inside Apprentice's of this constructed of these geocoder we must pass the context.
The context is main activity that this.
So instead of actually passing main activity that is we can't just say good application context right.
It's also possible to pass good application context and it's going to automatically specify the context
so the context is main activity right.
So that's it and now because the process of geocoding are actually our latitude and longitude coordinates
can actually fail.
We need to put our codes.
We need to put the process of Dakotan inside a try catch block so that they can actually catch the exception.
Otherwise our application is it may actually crash and we may have not be able to do anything.
So let's actually create a try catch plug so try create a try and then here at the after the curly brace
of this Troy.
Just leave it here catch inside the practice of this catch just tape in here.
Acception exception and the name is E and open and close.
This catch like by using these curly braces I just they've been here.
That print stack trace it can actually display somebody who's going out with somebody to the like so
that you can actually see what's been thrown.
So let's go inside the triple judge.
So let's create a list list and let's actually put a pair of angle brackets at inside angle brackets.
You want to specify the values that you want to put and should able to specify the data type of the
values that you want to put inside the list.
So now you may ask what is that list list is like an army list is not a class.
It is actually an interface and it is actually like an ad a but with more features.
OK we can do lots of things with lists.
So just look at this list as an adult.
OK.
So list and just put angle brackets inside the angle brackets unless specified that date or type of
the values that are you want to put inside this list which is going to be a fake address.
Look at this here address is inside and read that location package.
So just put it here address and the name of this list is going to be.
For example my addresses my addresses assignment operator.
So notice that because this is actually the list and this is an interface which can not create actually
an instance of type least by using the new keyboard.
It is not possible because this is actually an interface.
So let's see how we can initialize this interface.
So now it's time to actually forward to our geocoder object dots.
Let's actually for this method get from location name you can this you can see this one here and it's
actually set like this one and then person until January worth as you can see there are two versions
of this method here.
The first version that may actually show you again the first version actually accepts two arguments.
Actually the location name.
So it is going to be the address.
Right.
The user enters the address inside as it takes the first actually the EDT address name right.
It is the address the user enters the address inside that field.
And the second argument is the max results.
So it's going to actually turn that some Max resolve some results actually from the address for example
it's going to actually say you are here you are actually to somebody else.
So it's going to give you the most accurate addresses and the want five addresses.
For example 10 addresses or 20 addresses you can specify that here by using this Max results actually
argument.
Look at this second version of this.
If you have the option to actually pass the address name.
You have the option to pass the max odds.
You have the option to actually pass the lower left latitude you have the option to pass the lower left
longitude of the upper right latitude and longitude.
So for this application we just want to use the first version of this method is actually really good
and it's enough.
OK we don't have to actually pass the latitude longitude.
We just want to give our application the address and the address and the application is going to just
give us OK.
So you are here is going to actually determine your current location and it's going to tell you that
the destination address is there and then it's going to give you the distance the meters to get there
and it's going to give you the time that it is going to take to get there.
So now inside Apprentice's I can actually specify the location in which she is going to be destination
location address.
Right.
Come on at the max results.
I've been to actually only get four addresses so in this way we are initializing our list right.
Our list is going to have four accurate addresses the most.
Actually actually the most accurate addresses that are going to be only four and we can see that by
using by passing this address is going to give us those results.
So this is our list.
My address is Lydney actually create a new line here so that you can see what's going on here.
Now let me create an if statement so if my addresses actually is not equal to non If this list actually
has some addresses and it is not equal to null.
Then we can actually go inside the if statement and we can't say so remember that I told you that the
geocoder is going to actually convert that latitude and longitude coordinates to actually a human readable
text so that they can actually see what's going on here.
And now I want to actually pass the latitude and longitude but first I need to get the latitude and
longitude how we can do that.
Let's actually create a value of the double and the name is going to be latitude latitude assignment
operator.
And here I can refer to this least my addresses.
OK that's good.
So I want to get the value.
The first address actually that is inside the index 0 0 0.
Notice that we get four addresses so I want to get the address that is inside the index 0 dots get latitude.
I am actually calling this get latitude on the address inside the index zero of this my address list.
So in this way we get this latitude.
Let's actually create another variable of that double longitudes assignment operator my addresses that
get zero dot get longitude right.
Semicolon.
Now let's actually create a variable of type location here.
I'm going to actually let you know what am I creating this location variable but for now I just create
this value.
And then I'll tell you what's going on here.
Exactly.
So location and the name of this value and it's going to be address location address location address
assignment operator new location new location and you need to pass an argument to this actually constructed
of this location class.
So let's actually just going to be off base 3 and so just pass the value.
So let me show you what is the data type so location and you can see that it says street provider.
So we need to provide and should if you need to specify a provider which is of type string.
So I'll tell you it's not that important but it is called a provider is so that we can actually use
it later.
But for now in this application we are not going to use it so just pass the procedure here because of
the history.
I can't say my destination.
OK.
It doesn't really matter which value it is just past Ossining value my destination semicolon right now
that I have this location variable here which its name is location address.
I can say location address that set latitude and I have this latitude here right that I get from this
of this list so I can pass the latitude here latitude location address that set longitude and I can
pass the longitude here.
Right.
So I created a variable of type location here and I passed the profiler here and I said that the latitude
is this one.
OK.
And I said that the longitude is this one so I get the latitude and longitude from this my addresses
are from the first index the value that is under its index of this list and a past the latitude and
longitude of that address to the actually latitude and longitude property or instance variable of this
location.
Address right is valuable.
So now what am I going to do here.
I actually created.
This right here BELOFF take location because we have another class in our project called Paxi manager
the taxi manager class is our model.
What is the model the model is going to actually provide that data for us.
OK.
We cannot actually continue using the application without the model the model is the data source.
Without the data source the application doesn't have a data source and it is boring.
Right.
So let's actually scroll to the top.
Let's create a value of piked amount of type Paxi manager.
So now here after this line of code here in line number actually 41 inside the main activity class I
can actually say private taxi manager private taxi manager and the name of this rally is going to be
taxi manager Senechal all right let's go inside is uncreate Lifford here unless actually initialize
the taxi manager.
After this line of code that actually sets the listener on the spot it is Uncle close.
So I can't say taxi manager I can leave it to this variable taxi manager new taxi manager semi-colon.
So now I created a variable Actually I created the of the taxi manager here and I assigned this object
of Tove taxi manager to this valuable light.
So now this taxi managed valuable holds this object of a Pepsi manager.
So hopefully that makes sense.
Now let's scroll down here.
And here in this statement that I am actually taking for it not lability of this my address is here
after this line of code that actually sets the longitude of this location at least I can say taxi manager
I can refer to this rowdier but that holds the object of the taxing manager right that sets destination
location location.
Address right.
So what did I do here.
I actually passed the location address as an argument to this method that I created inside the taxi
manager class.
This is actually an instance method.
So it is going to be called on an instance of type actually Paxi manager class.
So taxi manager is an instance of the taxi manager class that sets destination location is going to
actually set the destination location.
So we passed this location address as an argument to this effect.
So it's going to assign this location address to the.
Actually this initial location instance variable of the taxi manager class.
So let's go to the taxi manager class here let me show you this your destination location right.
This is an instance variable.
And if you have the Smurfette here we created ourselves.
This thing is set this initial location.
So here this if it accepts an argument of a location so it's going to actually assign that argument
to the destination location instance valuable.
So let's go to the let's go back to the man with the class.
So that's what we are doing here we are passing the location address as an argument to these men.
And it's going to assign this location address to the destination location instance variable of this
taxi manager.
That's right.
So that's it.
Now let's see how we can use this object this model.
Right.
So now let's scroll down here.
And after the end in curly brace of this statement here right this statement that we created here inside
is unclear if it goes to him for actually not being equal of this address value and this distillation
destination location address is valuable here.
After the end in curly brace of this statement now I want to create another Actually I want to actually
put some logic here.
I want to add some lines of code here inside this on collegium efforts so before the end incredible
case of the Smurfette Let's actually see what what we are going to do here.
So because so you are already familiar with this because you want to actually access the user's current
location.
We must ask the user for the permission in order to access the user's location.
So let's create a valuable for that end and the name of this rally but it's going to be permission check
permission check assignment operator and I can't say context compact context compact that check self
permission.
So for the first argument we need to pass the context so I can just say good application context.
Come on.
And as the second argument we need to specify the permission.
So I can't say manifest that permission.
That course location access course location.
So let me actually create a new line here so that you can actually see the entire effort here.
So now that we have the permission check let's create and if statements.
OK first let's actually put a semicolon here at the end of our statement.
And now let's create an if statement so if permission check is equal to package manager that permission
granted the Capitol right that permission granted or just need to open and close.
This move this statement by using the school libraries so you can see that if the permission check is
equal to a package manager that permission granted means that we have the permission to access the user's
location.
OK.
So let's create an exit statements or else it just means that we don't have to actually get permission
to access the user's location.
So let's request that let's ask the user to give us the permission to to access the location.
So let me paste this line of code here.
As you can see I paste this I pasted this line of work here that says activity can pass that request
permissions.
First we need to pass the context main activity that this and as the second argument we need to specify
to actually the permission as an addict.
So new streams A.
And I just passed the first actually the first value this is going to have only one value which is going
to be accessed course location and at a permissions right because you want to have only one actually
permission in this application you won't be able to actually access one.
A dangerous mission for example.
So I just passed this value here to manifest that permission that the access course location.
And here it is actually that request code.
So it is ok to be number one.
Right.
And now that we have actually requested the user to give us the permission here before this one image
that I can actually say for example takes the distance that it takes two coats.
And here I can say this up is not allowed is not allowed.
To access the location right now let's actually go inside this if statement so what happens if the user
actually allows us to access the location.
So now we want to put that logic here inside this statement.
So for that we need to put we need to actually to use the main entry entry point in order to access
that user's location which is actually called fuse location provider API.
This fuse location provider API is actually the main entry point in order to access the user's location.
So let's actually name it the same fuse actually fuse location provider API write assignment operator
and and here I can actually say location services that used location services that fuse location API
right semicolon.
So it may actually create a new line here.
So that they can see what's going on here first.
We created a bit of type fuse location approach by the API here and we assigned this value to this valuable
location services that fuse location API.
So now we have that main entry point.
So now I can say so now we can actually access the user's location.
So lets do that.
So it lets you to create a variable of the location and the name of this value is going to be user current
location assignment operator.
And here you can see fuse location pro-white API that gets last location.
OK good.
Lastly location and we need to pass the Google API client to the Smurfette So we have an instance of
that Google client here semicolon.
So thats it.
And this way we can actually access the users actually last location and we can assign it to this valuable
user current location.
So now you want to actually create an if statement and I want to let you know about it.
A very important point about geocoding in Android.
So after this line of code that actually gets the use that was current location I can say if user current
location is not equal to null OK.
And this is actually an ontological OPERATOR Right.
Is geocoding So here this is very important let's actually be open and close this if statement by using
these curly braces and often incredibly is followed by a closing curly brace.
So here I am actually thinking that if this use of current location is not equal to not and is geocoding
is geocoding here means that if the value of this is geocoding variable is true and the user is actually
to caught in it.
Right.
So there is a problem here.
Let's actually scroll to the top here and see that we have this try catch block right.
So here we are actually change for the condition for the process of coding.
So what happens if geocoding actually goes wrong.
OK if the user can actually cannot for example the location of the user cannot be of the address of
for example the address that that the user has entered.
For example in that case that address cannot be for example found right.
The geocoding could go wrong.
So we need to go inside is Cheche plug.
And we need to say is geocoding false right semicolon.
So inside it is catchable.
We must assign the value falls to this is Joe geocoding so that the application is not going to code.
If the geocoding is actually going wrong so that it's not going actually it's the device's battery it
is very important because it is very it is a very hard working process.
It's going to be used as a battery.
It is going to it the device is battery.
So here you can see that initially the assigned value truth is valuable.
So whenever they use it actually clicks on that button whenever they use that actually touches that
button.
Get the date of this if it is going to be caught.
So this means that the value true is going to be assigned to this is Joe geocoding.
But if the geocoding goes wrong it can actually execute this catch block and it's going to assign the
value falls to these is geocoding valuable so that it's not going to actually court anymore.
OK.
And as a result it's not going to actually eat the use that as battery.
So now let's actually scroll down here and here is for this statement We are thinking that if that is
current location is not equal to an ad is geocoding is actually true.
So this means that it didn't actually run.
So it actually works perfectly here.
Now I can't say inside this statement takes the distance that said text that said text.
And now I can actually access the model.
OK the model object that we created Paxi manager thought.
And you can see that we have these two methods right.
This one return the miles between the current location and the destination location here.
And you can see that this Smurfette accepts two arguments.
The first one is the current location of the user.
So we have that to use a current location right so I can just pass that value here use their current
location karma.
And now we need to pass the meters per mile.
OK.
So the meters per mile is a value that we are going to get from our.
Eddie takes.
Let me show you the emulator.
So this is our emulator and this is actually the term meters per mile.
Right.
So whether the user actually enters our value here that value is going to be passed as this argument
as the second argument meters per mile.
It is going to be off the.
But the value that the user actually enters inside is as it takes is going to be off the street.
So we need to actually convert that string value to an integer value.
Lets see how we can do that.
I can actually use the class Integer as you can see the or it is capitalized the right integer to parse.
So this is actually the static method of this integer integer class.
Right.
And instead Apprentice's of this parse into method I need to pass a value of type streambed.
So it is going to be converted to a value of integer.
So I can just say EDT actually meters per mile Dud's get text that it takes that to string.
So it is actually a very long method but as you can see I have that extra Apprentice's here.
So now it is actually correct.
So it is actually a lot to learn but it is very simple it may actually create a new line here.
So as you can see we have actually this taxi manager and we are calling this movement and its taxi manager
object returned the miles between the current location and its destination location.
So we passed that they use that kind of location as the first argument.
The second argument is going to be to actually the meters per mile.
So because the user is supposed to enter that value inside it it takes that value is going to be off
the street.
So Vinita actually converts that value to our and to an integer value.
Right.
So let me tell you that they use it when the actually the user enters a value inside these editrix.
It is not actually necessarily of types 3 and it is often editable.
OK.
So if you want to you need to actually convert it to a string value.
So thats what we are doing here.
We are getting the value of this EDT meter's pyramid that it takes two to three.
And if you are using this method parse it on the integer class in order to convert a string about you
to an integer value here.
Right.
And here there is a problem and the problem is that the problem is actually solved.
Its not a very big deal but the problem is that we are assuming that the user actually enters a value
of type integer here so that it is actually convertible to an integer value if they use it.
For example Intel is of value for example of types 3 and our application is going to crash in the neck
in their previous tutorials of this course I taught you how we can actually use the try catch the exception
handling techniques in Java in order to prevent those kinds of crashes in your application.
OK so I'm not going to cover that here too because you already know about that you already know how
to use the try catch here.
You can't just put it create a try catch and inside the tribal touch you can actually use this piece
of code and you can convert this string value to an integer value and instead the catch block you can
actually just catch the exception and your application is not going to crash.
OK.
So that's it you are converting the string value to an integer value.
And now this actually puts the second piece of code here.
So after we actually setting some text to this text the distance I need to just type in here takes the
time.
OK that set text.
And now again I can't say Paxi manager that returned that time left to get to destination location.
So this faith accepts actually three arguments.
The first one is they use their current location so I can pass that here use current location comma.
OK so now in order to show you the arguments that me actually call it again.
So I go in.
So now you can see these arguments here.
The first one is the use of current location comma.
The second argument is the miles per hour or three miles per hour.
So we have an eddy takes for that.
Let me show you the emulator.
This is actually this second Ed. This is actually that is 1 mph.
So again we need to actually have a value of type.
What we need to have a value of float and the value that actually the user enters inside is editrix.
It is of tide actually stream.
So we need to actually convert that value to our two and two offloads value so we can actually use them
in the float class here and the Fed argument is actually that meters per mile.
So let's actually specify the second premise here float.
Let's use the float class you can see that the F is capitalized.
Right thought Parsa float and I can pass a value of type string to this method.
So the value is EDT.
Actually miles per hour that get fixed that two is three right.
So this is our second argument here.
Let's actually put a comma here.
And are actually our first argument is going to be bought.
Let's actually put the comma here.
OK.
So because this second argument is he had floated parts float at the end in private practice this year
is actually related related to this starting Prentice's of these parts float here.
Right.
So now let's specify this.
The first argument actually the argument is the meters per mile that is going to be off that integers
so I can see integer class that parse.
And inside the apprentice's I can just say EDT to the EDT meters per mile.
That's good.
Text that to a string.
Right.
And finally I just need to put a semicolon at the end of my statement.
And here there is actually an error.
So let's let's create a new line here so that we can actually see what's going on here A.J. the person
in it is meter per mile that takes that restraint.
So we need to pass another.
And in practice this year.
So actually the last Prentice's was missed.
So now it is here and everything is working fine.
Here let me actually create another new line here so that you can actually see what's going on here.
It is actually a very long method.
So let me explain again.
We have this taxi manager object we are calling this Smith had returned the time left to get to the
destination location so it can actually give us the time actually lift to get to the destination location.
This it accepts the argument arguments.
The first argument is they use a complication.
The second argument is actually the miles per hour that the user is supposed to provide for our application
in order to actually calculate the time that is left in order to get to their destination location.
And the first trimester actually the argument is metered as per mile.
So it is actually here in Mr. paramount.
And we are actually converting these values to their specific data.
So for example this one is an integer value so if you call it is a class the.
And this one is a float value.
So we called float that float.
So that's it.
And everything is fine.
Let's launch the application on the emulator here because I want to show you something very important.
So now we can see that our application is actually running on the simulator and it says for example
enter the address and tell the address here so I can actually for example and enter an address.
So for example I can say Los Angeles OK Los Angeles.
And it says Enter miles per hour.
So I can say for example six feet and 10 meters per mile.
So I can say for example 40.
Right.
But here let's actually just disappear at this virtual keyboard.
Let's click on the spot and get the data and let's wait.
As you can see nothing happened here.
Let's click on it again.
Nothing happened.
So at this point I want it actually to let you know that as an Android Developer you may always encounter
bugs in your applications.
OK.
When you want to actually for example create a very complex Android application you may always actually
encounter different kinds of box.
And the most important lesson here there is that do not panic.
OK.
You just need to find the bug and you are going to actually eliminate that bug.
And the application is going to work to work fine.
OK.
Do not panic.
So our application has a bug.
As you can see our application actually didn't respond.
Nothing happened.
So there is a bug.
I just need to find that bug.
And the first important point that I want to tell you let you know is that there's that.
But these have happened in the event that actually exists.
So I actually pressed on that get the data button nothing happens right.
So the bug must be inside the actually on each method.
OK.
That's what I'm guessing.
OK.
I'm not actually 100 percent sure that the bug is inside this method.
I'm just guessing that the bug is inside it and if it inside the curly braces of this Anchal each method.
And let me tell you that the bug is in line number 82.
OK.
Instead of initializing the destination location address I mistakenly.
And actually I just did it on purpose.
OK.
I just wanted to let you know about the not panicking when you actually encounter a bug in your application
and you just need to find that Bhagvad it actually exists and just eliminate it.
Right.
This is actually called debugging capability.
I want to increase your Akiba it debugging capability so that the bug happens here actually although
our application didn't crash but it didn't work fine also.
So let's actually solve this bug here instead of assigning destination location address to this out
of this value I need to say this.
Let me actually comment on this line of code here.
I need to say destination location address.
This is actually an instance variable that we created inside this main activity class assignment operator.
Now I can assign address value to the destination location address.
Right.
Because in line number 76 inside this uncle if are getting the value of this it is the address here.
And we are assigning get to this address value which is of type string.
Inside this statement we want to sign that at least value as the destination location address so that
these two values are actually equal.
Ok the same.
So now let's run our application again.
Let me actually stop the program and let me run the app again.
And let's actually choose the emulator and let's click on OK.
Let me show you the emulator.
So now our application is running.
Let's again enter an address.
Los Angeles enter mph so I can say for example 60 centimeters paramount so I can say for example 40.
Right.
Let's actually disappear.
It's actually a virtual keyboard a keyboard disappeared.
And let's click on button get the data.
Let's wait and now again nothing happened.
OK.
So why actually nothing happened here because we are actually using this app on an emulator.
OK.
And it emulator is not actually able to actually get the use of its current location so that we cannot
actually specify the distance for the time to get to the destination location which is actually Los
Angeles.
So there is a problem here because the location they use a current location is going to be null and
it's not going to actually specify any values for these two takes viewers.
So let's actually test this out on our own actual device so I'm going to test it on my own actually
works and let's see if this actually has any box or not.
Let me actually stop the program now.
Let me run the app and this time I'm going to choose HTC Desire.
This is my actually my own view device.
And when we went and with the let's get to kind of OK.
So I tested this up on my own actual device and it works perfectly OK.
It actually gives me the miles and it also gives me the hours and the minutes.
So that's enough for this tutorial.
But let's actually also delete this line of code that actually that was the previous book.
So I think that's enough for the storyline in the next story or we can actually continue adding functionality
to this application.
So I want you to actually start creating your own location based application.
I want to actually post a discussion on your application to the course and also make sure to actually
share your quotes with other students.
Make sure to use your own creativity in order to actually create a location based application based
on the knowledge that you have here.
So the nice tutorials we can actually add more functionality to this application and it's and are going
to actually make it a production ready application.
Thank you very much for watching and I'll see you in the next tutorial.