Integrating Google Maps API with Unity

Getting Access to The Eye in the Sky

Micha Davis
Nerd For Tech
3 min readJul 3, 2021

--

The least they could do is say “please”

Hey, I’m back from my back-woods hiatus: if you’re reading this, support and protect your local wilderness preserves!

Okay, enough of that. Today we’re going to add the ability for our insurance app users to grab a static map of their current location from Google Maps and add it to their new claim. Let’s dive right in.

If you’re following along, for this process you will need a Google Maps API key.

Script details hidden because SHHHH API Keys are secret!

To begin, I’ll go to the C# script for my Location Panel and create the necessary variables to create the URL to request our map and to process the information for our form.

These variables are public because this is a demo and it’s less clutter than serializing each field to view it in the editor.

The URL is formatted according to the instructions provided by Google. To construct it, we simply build a new string out of the variables using the parameter keywords provided in those instructions.

But in order to get the values for latCoord and longCoord, we’ll need to first make sure we are using UnityEngine.Networking.

Unity has Start() and Stop() methods for location services so you can precisely control when your app is actively trying to know where the device is. We only need it for this little section of code, so that’s the first and last part of the process. Once location services are on, we loop through a 20 second wait, checking each second if we have a result from Google. Depending on what happens we’ll either log an error, or request the data we need for our variables.

Here’s the pseudo code:

If location services are enabled
…Start() location service
…set a max wait int
…while status == initializing and max wait > 0
……wait for a second
……max wait -1
…if wait is 0, log “timed out”
…if status == failed, log “unable to determine location”
…else, get lat and long
…Stop() location service
else log “location service not enabled”

Since we’re waiting for seconds, we’ll need to put it in an IEnumerator type method and use the yield statement. We want the map to load immediately, so we’ll make void Start() into IEnumerator Start():

Finally, we’ll construct another coroutine to fetch the map data from Google using the location data we’ve obtained from the user’s device. We construct the URL (as above), and then we enable a Unity Web Request. Instead of a while loop, we can yield return on Unity’s SendWebRequest() method. If that method returns an error, we’ll log it. Otherwise, we assign our image texture to the image we downloaded.

In pseudo code:

construct the url and parameters
open a unity web request to get map texture from the url
…wait for result
…if result is error, log error
set map UI image texture to downloaded map image

And in C#:

That’s the whole process. In the interest of not showing you where my basement lair is located, I won’t display the results of the code — but it works!

Tomorrow I’ll go over a very neat free plugin for Unity that can grab an image from any camera on any device. We’ll use that plugin to allow our users to take pictures of the scene of the claim.

--

--

Micha Davis
Nerd For Tech

Unity Developer / Game Developer / Artist / Problem Solver