All right, so the second lesson is about simplifying your code and trying to always go on the simple
route and also trying to avoid reinventing the wheel.
This is very important.
So we are hearing the restaurant's activity, as you can see, and we want to refactor something in
the filter restaurant's method that we just extracted in the last class in the last video.
All right.
So if we look at this map, there is something that I really don't like.
And I think that you don't like it either when the filtering of the restaurant is is actually done.
As you can see here, there's the calculation of the distance between the user and the app in the apps,
restaurants, of course.
And you have all of this crazy logic with calculating, considering the radius of the earth and all
that.
And I guess that the logic is fine, right?
I mean, it works.
The only problem is that we don't really want to have this kind of logic.
It seems that we are reinventing the wheel and we can just try to use something more simpler, something
that comes out of the box.
So if we go to Google and we just type in calculate distance between two locations on Android, get
to these results.
And I think that this one, if we look going here, we can see that we have here a pretty similar approach
to what we have so far.
It's pretty long.
And as I said, calculating the distance between two points seems to be a task that has obviously been
approached by somebody else until now.
So there's no need for you to reinvent the wheel and create a custom implementation.
You can always use something that is already done.
And on the second answer here, I should the first answer there is something really cool.
And this is actually somebody saying this, that there's no need to reinvent the wheel for such calculation
between two locations.
And there's an out of the box method here called location.
The distance between that does exactly this.
And it's pretty simple.
It has three lines or something.
Let's copy paste this.
Right.
And let's go back and delete all of this crazy code that we have, actually delete this part here and
let's leave the log for now and delete this as well.
We don't really need it.
And let's go here.
Let's paste what we have just gotten from the code and asking me that maybe he could convert it to.
That would be very kind of you.
Andrei's to you.
Thanks for doing this.
And it has already done this for me.
It obviously asked me to import this, and I'm going to do that because I want to do this and look how
cool this is.
Also the return type here is not really what we want.
So let's call this vault distance here.
Sorry.
Well, let's call this what this does and.
Like this, all right.
Actually, not distance, let's call this distance result here, and this is a result here, is going
to take the actual result for from this distance between and I guess that we will receive this in meters.
So let's divide it by one thousand just to make sure let's go back here and base the first latitude
as the user latitude and longitude as the user longitude and do the same for the restaurant here.
So let's go.
And they feel to the restaurant door latitude.
Actually it's first distance now is ducation.
I got confused a bit here and select latitude and also the same thing we have to do for the longitude.
Let's see here the same thing and select here longitude and perfect.
Right now we have the calculation done as passing distance array, which is an array.
And actually this map is going to change this distance array content.
So when we are going to access the first element of the out of the distance array, we are going to
get the actual result.
So right now, let's also copy this here in this log and look how clean the actual logic of creating
inculcating this distance is right now.
Let's also make sure that the logic works fine on the map.
All right.
So the distance has been low, but we still forgot something.
We have zero kilometers.
So what we have to do is actually set this distance result to the filtered restaurant field field.
So we'll go here and say distance equals distance result and that's much better.
But we still have an error now telling us that, hey, we need a internat afloat.
So let's go to entier.
Just we don't really care about the accuracy of this.
And right now, the distance has been calculated properly, so we have refactor the code.
The app is working fine and we have cleaned the actual code of doing this on this crazy computation
here.
So the first thing to remember is that we should always take the easy route when there is an easy implementation
that has been already done.
Don't reinvent this.
So before moving into the next lecture, let's do a bit of summary on what we just learned.