How to retrieve static map from Google Maps in Unity

Niklas Bergstrand
3 min readAug 13, 2021

Let’s go over how to use the Google Maps API to download a static map from your current location.

First, we need to setup a developer account with Google. Go to this URL https://developers.google.com/maps/gmp-get-started. Click on Get Started and follow the on screen prompts to create the account:

Once logged in on the developer account create a new project:

Enter project name:

Select map product and click next. You will get a pop up that shows you the API key. The API key you will need later to retrieve the static map from Unity:

Open up Unity and create a script for the UI panel that shows the map. The image on the panel needs to be a raw image and not a standard UI image:

Open up the script. Add the using directive UnityEngine.UI and UnityEngine.Networking the following variables:
-string for the API key
- RawImage of the location image
-double for the coordinates
-int for the size of the image
-int for the zoom level
-string for the URL

Change the Start function into an IEnumerator. Start the location service and check if location services are enabled on the users device. If enabled set a max wait time. If successfully initialised set the coordinates and then stop the location service. Start the coroutine for loading the map:

To load the map we need to put together the URL with all the parameters. The final URL will look similar to this “https://maps.googleapis.com/maps/api/staticmap?center=0,0&zoom=12&size=400x400&key=*API KEY*”. Inside a using statement create a new UnityWebRequest using the URL. Set the texture on the UI to the texture from the UnityWebRequest:

Lastly, add everything to the script on the inspector:

Good luck!

--

--