Welcome back.
Now let's call this function.
So open screens Uber clone map.
And here scroll down.
Let me set for that actions API.
Start with val route points by ViewModel route points dot collect as state with lifecycle ViewModel
dot route points accesses a state flow.
List of Latlong property from your ViewModel.
This holds the current route coordinates or null if no route the collect as state with cycle.
This is extension function from Android x dot lifecycle package, converting the flow into a compose
state object.
So here we are.
Converting the flow into state object automatically starts and stop collection based on lifecycle collects
when life cycle is at least started, stops when life cycle is stopped.
Replace the last emitted value when recomposing and by keyword.
Kotlin property delegate syntax automatically handles the state subscription triggering recomposition
when the value changes.
More efficient than regular collect as state function.
It stops collecting when the UI is not visible.
Resumes when the UI returns to foreground and preventing unnecessary background work.
Your composable will automatically update whenever new root points are emitted, then create a new variable
for the selected points.
So val selected location by ViewModel dot selected location.
We're going to create it dot collect as state.
Let me create this variable in the view model.
So back to here create private var val.
Similar to what we've done in the route points we're going to create for the selected location.
So selected location equals to mutable state flow of lat long.
And initialize the variable that's initial state to null val selected location selected location as
state flow.
And this convention that underscore this is for and the selected location.
This is for encapsulation.
So this will be only changed inside this class.
And the selected location is only for read only operations.
Function set selected location passing location as a parameter.
It's of type lat long.
Use selected location dot value and set it to the passed parameter.
There is a very important note here we have created before.
The destination marker class, which is relying on a lat long and a latitude and longitude with additional
title and ID variables.
Also, you can use this function to lat long in order to convert the destination marker to lat long,
so you can use this function in order to convert the location into lat long variable back to Uber cloud
map.
This is the selected location.
Now create var marker position by.
Remember mutable state of lat long and set the initial variable to null.
Now scroll down to that Google map and here inside the parameters of Google Map composable, we need
to allow the user to draw marker.
So on map click go and create a marker.
I'll name it as my marker.
I'm going to use this marker.
It's of type lat long, so set the marker position to my marker and call the ViewModel dot.
Set selected location to my lat, long or my marker.
When the user clicks on the map, go and create this my marker lat long object and get the value of
this lat long and store it inside the marker position.
Now we need to get and use this marker position.
So inside the scope of the Google Maps go to here.
Scroll down and the marker on.
Click position on map.
So marker position dot If it is not null, go and create a marker.
Composable having the following parameters.
The state marker state.
The position would be it title.
Selected location.
Snippet.
Display the coordinates and when the user clicks on this marker, go and set the ViewModel dot.
Set selected location to this marker position.
Okay, it's very simple now and return false okay, this is the marker position we need.
The smart cast to lat long is impossible because marker position is delegated property.
So here we can make it as lat lat long.
Or we can use the overloading methods.
But this is good if you add this exclamation mark.
And this is good.