In this article, we will continue receiving location data from user’s device and display it on app. At the end of building this function, you might need to install the app on your own device to check the result.
Get the coordinate
After we enabled Google Map Service and request the data from url in the previous article, we will get the location data by using LocationService.
First we need to make Start() as an IEnumerator, and inside this coroutine, we have to finish the process step by step.
At the beginning, we need to check if user has authenticate the permission of using location data.
If user has authenticated the permission, we can move on to start the location service.
Start the service with one line: Input.location.Start().
Then we need to set a timer as a maximum waiting time of initiate this service. We will use a While() loop to do this part, and make it return error message if waiting for over 20 seconds.
If location service is failed to initialize, send the error message.
Otherwise, the location service has successfully receive the location data, and store it to the variables of coordinate.
When we received the data, stop this service.
At the end of this method, we can send error message when user has not enable location service at the beginning. Use it as else statement under checking the if statement of enable location service.
All done! This would be the whole Start() looks like when finish this part.
Unfortunately, we could not use this location service in Play mode, you need to build this app to test it.
Once you successfully installed this app, make sure enable the location service in the Settings, and start the app.
If you can see the current location on your app, you did a wonderful job!