How to retrieve static map from Google Maps in Unity

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!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store