Integrating Google Maps API with Unity
Objective: Get a picture of a map of the user’s location.
To display a map, we first have to get the user’s location from their device. We’ll need to use an IEnumerator method to do so; I’ll use the Start method in this case.
First we’ll make sure the user has allowed our app to access their location.
If we do have permission, we’ll tell the system to start the process. We’ll give the system 20 seconds to get a response before we give-up.
If the system can’t get the location, we’ll report that.
If we do get the location, we’ll save the latitude and longitude.
Regardless of the result, we’ll have to stop the process we started.
Now that we have the location, we’ll call another IEnumerator. We’ll begin by assembling a URL string with all the data we need: the coordinates (the location point will in the center of the map), the zoom (how focused into the location we the result to be), the image size we want, and our Google-assigned Map APIKey.
Then we’ll use a using statement to make a UnityWebRequest that will store the result of the URL as a Texture. (The using statement will clean-up after itself when finished.) We’ll then send the request. If we get an error, we’ll report it. Otherwise, everything worked and we’ll apply our texture to our image, displaying the map for our user!