Integrating google maps API with Unity
Right after pressing the next button after entering our name, we’ll be taken to a map screen to select our location.
This will be provided to us by google and there will be some preliminary setup with them needed. First you’ll need a google maps api key
Getting my own API key, I entered a new project
After creating the project we’re taken to an overview
We want the map static API which is 2nd on the last row.
And I guess we hit enable.
and now it shows we have it enabled in our list of available apis!
Under credentials, you’ll be given a secret key and an example on how to access a map via URL, we’ll get more into this later.
time to get coding! We’ll first create a variable to hold our api key in the LocationPanel script.
and then paste it in in our inspector. I won’t show that here for obvious reasons. Next, we’ll need to format the map url via googles instructions.
So we can send a url with parameters, fortunately googles instructions provides an example of them that we can model after.
So given the example, we’ll need variables for:
- x coordinate
- y coordinate
- zoom
- image resolution
Next, we’ll create a string variable to hold the base URL, and we’ll reconstruct it using the variables next.
And now the programming magic! we’ll concatenate the url with the necessary parameters from the variables using the example as a reference:
Next we’ll load the actual map from google, it’s a bit complicated but we’ll start with a coroutine:
We’ll be using Unitys own 2020 example of UnityRequestTexture.GetWebTexture to base our map downloading code off of. Basically, I replaced uwr with request so it’s a little more readable:
Basically what’s happening is a web request with the variable name request is made, and we’re telling Unity to request the texture based off of googlemaps url. Yield return SendWebRequest will return only when it’s finished, if the request isn’t a success, we get an error, but if it does download successfully, we put the downloaded image in a variable called texture, and then change our image texture to the downloaded one.
And sure enough, we now have a map loading from google maps! Huzzah!
Finally, we’ll get the coordinate info from our mobile device. First we’ll check Unitys page on location services.
According to the example, we’ll need to use the Start function as an IEnumerator, so let’s create that and move our code from the Awake function into it while we’re at it. Actually, I just put it into a function called DownloadGoogleMaps();
And now we have an empty Start function (converted to an IEnumerator)
Let’s fill it up! Here’s the entire chunk of code, I’ll explain things line by line after:
The first line is just filling out the case number at the top of the form. Next we’re going to need to ask permission from the user to use their GPS location services.
After permission is requested, it will check to see if it was given, but just to be sure, we’re going to give them around 4 seconds to hit the yes button, otherwise, location services would be started with or without permission and if it’s started without…well…we won’t have a working panel, unfortunately.
After that, we’ll wait another 4 seconds for the location services to start and initialize, usually it’s 20 seconds given but that’s a little too much in my humble opinion.
The next few lines are for what happens if the service doesn’t start up within the alloted 4 seconds. If however it’s successfully started, we’ll assign the latitude to xCoord and longitude to yCoord.
When we’re done using the service, we’ll stop it. No need to use up valuable resources! the else is just what happens if ‘isEnabledByUser’ happens to be not true. After we’ve gotten the x and y coordinates, we’ll download the map from google using the URL we pieced together with all of the variables that now include our x and y coordinates.
Unfortunately, I don’t have a screenshot for you, but trust me, it works! But this has been my experience in discovering how to get google maps working in my Enterprise app! Hope it’s been helpful to you as well!