A Guide to Using Google’s Maps SDK for Unity 3D - Part 1

Part 1: Project Setup

Kristopher C. Tobiasson
Everdevs Community

--

Image by digital designer from Pixabay

Note: The Maps Sdk from Google is now deprecated. Some readers have inquired about google’s pricing of the gaming API’s. Don’t take my word for it because I don’t work for Google, but as of this writing, Google gives you $200 free credit every month and that equates to having roughly 20,000 daily active users (DAUs) per month. Afterwards, you are charged $10 per 1000 DAUs / month up to 1,500,000 DAUs, thereafter it drops down to $8 per 1000 DAUs / month and there is a note to contact sales for volume pricing when you get to 7,500,001+ DAUs. I recommend that you take a look here for a thorough explanation on the pricing, the gaming section is at the bottom of the page.

Over the past year I’ve been developing a real world running game with Unity 3D for Android and iOS. Needless to say, location based game development and I became as thick as thieves in the process, testing each other every step of the way, right up to our recent release.

Although the initial idea was to use Google Maps, their Maps SDK was not released to the masses until about 4 months after we started development. By that time, I was knee deep in another popular mapping tool from a company named Mapbox and there was no going back at least not until after the first release of the game.

The Mapbox SDK stuck around with us until the release of version 2.0.0. Thereafter, we decided to do some tests to see what the newly released Maps SDK from the Google Maps Platform was made of. After some thorough test runs, I realized that Google’s Maps SDK was far more suitable for our project.

Time for a boat load of refactoring to be done! 😅

The Maps SDK from Google is brilliant. After taking some time to get familiar with it, you will see just how powerful it can be. Getting setup and understanding the basics is really the biggest hurdle in the way.

I scavenged the internet in search of some kind of a step-by-step guide which would help with the initial learning curve of using the SDK. It became apparent that since the SDK was so new there was really no such guide available. Ultimately that is what pushed me to create this guide. I hope you find it helpful.

Enough backstory, let’s get to it!!

Create a New Project in Unity:

Before creating your Unity project, I recommend getting the latest LTS release of Unity which is version 2020.3.4f1 at the moment. If you prefer to use an existing project on a previous version of Unity or you have some other reason why you don’t want to use the latest LTS release, then just know that the Maps SDK requires that, at the very least, you are running version 2018.4 of Unity as of this writing.

Download latest LTS version of Unity

Once you’ve chosen the version you will be using, create a new project with the regular 3D template and call it whatever you like.

Create a new Google Project and get an API Key:

Now we can pop over to the Google Developer Console and create a new project so as to grab ourselves an API Key ( Google won’t serve any map data if you don’t supply an API key setup with the correct API ). You will need a google account before you can create a project in the console and a personal Gmail account will work just fine.

Login to your google account and then head to your Google Developer Console dashboard. If this is your first time in the console, you should be shown the following popup with this meet and greet which prompts you to input your Country and to agree to the Terms of Service.

Add Country and agree to terms

After you have agreed you should be in your dashboard which has a blue navigation bar at the top. On that navigation bar, click “Select a project”.

Select a project

The following popup should be displayed. Click “NEW PROJECT” and give it any name you want. Don’t worry about the “No organization”, it is just because Google thinks you’re very disorganized… Ok ok, this is just if you have one or more organizations and you can set the project to be a part of one of them.

Create a new project

If you’re new to the Google Developer Console or you haven’t already activated your free trial, now might be a good time to do so. There should be a bar at the top of the screen which looks like the following.

Who doesn’t want free credit

So now let’s click on the little hamburger menu expansion button in the top left corner and scroll way down to the “Google Maps Platform”and in the sub menu, select APIs.

Check out available API’s in the Google Maps Platform

There should be quite a few APIs on the screen, but the one we’re looking for has never been present during my visits. Go to the search bar above and type “Semantic tile API”. And click on the API in the search result below.

Semantic Tile API

On the next page, click “ENABLE” to enable the API. Thereafter, you should see a message which declares that you may need credentials to use the API. Click “CREATE CREDENTIALS”.

Create your credentials

On the next step, select the “Semantic Tile API” and “Public data”. Click “NEXT”.

Select the API for the credentials

Now an API Key will be generated. Copy the key down somewhere as we will need to use it in Unity shortly.

Copy your API Key

It isn’t one of those keys that you only have one chance to save because you can always come back to your account to get it. Having said that, you should still keep it safe and private.

Semantic Tile API Key

As the warning suggests, before using the key in production, it is recommended to restrict access to the key. This is so no other apps can use your key to extract Semantic Tile API data which will cost you money!! Restricting the API key is out of the scope of this tutorial as we are not going to use the key in production, but keep it in mind before launching any app which makes use of your API key as it is an important security measure to implement.

Get the Maps SDK and Test it Out:

Let’s go ahead and install the Maps SDK and unzip it’s archive file. If you’re using a version of Unity which is between releases 2018.4 and 2019, then you would choose the 2018 download link, if you’re using a version which is 2019+, then you should choose the 2019 download link.

Download the respective SDK

If you don’t have the Unity project open, open it up and at the top of the screen select Assets/Import Package/Custom Package… Find the Maps SDK package folder and import both the examples package and the Maps SDK package into the project.

Import package

In the project panel, go to Assets/Examples/01_GettingStarted and open up the “HelloWorld” scene.

Open HelloWorld scene

In the scene’s Hierarchy panel, select the “Basic Example Script” game object and add your API Key from Google to the API Key field highlighted in red.

Include your Semantic Tile API key

Now press play and you should see the Statue of Liberty. Move around with WASD, QE and the arrow keys and check out different GPS coordinates by changing the Lat Lng field in the “Basic Example” script. Furthermore, if you dig a bit through the Google Maps Example folder, you will find a lot of scenes, each showing off a different aspect of the SDK. Just make sure to paste your API key in the Maps Service script in any scene you open up.

Apparently she stands for liberty

Well that’s a major step out of the way. Although we haven’t taken a dive into the actual functionality of the SDK, we can now dynamically load the surroundings of any GPS coordinate in the world. That’s pretty cool!

We’ll get our hands dirty in the next one with our own custom map scene.

Ready to head over to the next tutorial? Part 2: Map Scene Setup

I hope you’ve found this tutorial useful. Feel free to leave feedback in the comments below.

--

--

Kristopher C. Tobiasson
Everdevs Community

Professional Freelance Unity 3D / Full Stack NodeJS Web Developer | Founder of Everdevs.com | Diehard Dad