Using GPS and Google Maps API in Unity

Thank you for your hard work.
This is Matsuyama from the Yokohama office, now the System Development Department.

It's been a while since my last post.
I'll move away from AR for a moment and write about GPS functionality and Google Maps.
Let's try making something like this.

What to make

- Get your current location using GPS
- Display a map image centered on that location
- Update the map at regular intervals if you are moving
- Display an icon at the current location on the map

Obtaining location information

First, location information can be obtained using the LocationService class.
Reference LocationService

First, start updating your location.
While you're there, enable the compass function.
Reference Compass

Input.location.Start(); Input.compass.enabled = true;

Since information cannot be obtained until the program is started, it will wait for the following judgment

// GPS is not allowed if (!Input.location.isEnabledByUser) yield break; // Wait until service status is running while (Input.location.status != LocationServiceStatus.Running) yield return null;

If GPS is not allowed, there's nothing you can do, but just in case

By the way, please note that if you do not set the Location Usage Description in ProjectSettings,
you will not be able to obtain location information on the actual device!

Location information (latitude and longitude) can be checked using lastData of LocationService

LocationInfo curr = Input.location.lastData;

Once you have obtained your current location (latitude and longitude), you can obtain a map image from those coordinates

Map display

To use Google Maps, you must first register with Google Maps Platform. (No explanation required.)
Google Maps Platform Official

If you want to display a simple map image, you can use the Google Static Map API, which
will retrieve an image centered on your current location, obtained using GPS.

① Specify parameters in the URL

// Base URL string url = @"https://maps.googleapis.com/maps/api/staticmap?"; // Center coordinates url += "center=" + curr.latitude + "," + curr.longitude; // Zoom url += "&zoom=" + 18; // The default is 0, so set it to an appropriate size. // Image size (up to 640x640) url += "&size=" + 640 + "x" + 640; // API Key (key issued by Google Maps Platform) url += "&key=" + GoogleApiKey;

② Download the map image with UnityWebRequest

url = UnityWebRequest.UnEscapeURL(url); UnityWebRequest req = UnityWebRequestTexture.GetTexture(url); yield return req.SendWebRequest();

③ Display downloaded images as sprites

// Create a texture. Texture2D tex = new Texture2D(MapSpriteSize, MapSpriteSize); tex.LoadImage(data); // Create a sprite (instance) dynamically. mapImage.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);

Then,
we run this process in a coroutine to update it at regular intervals.
There is no need to update the map all the time, so we will update the map only if the coordinates have moved a certain distance from the previous coordinates.
The distance was calculated like this:

///<summary> /// Distance (m) of 1 degree of latitude (longitude) ///</summary> private const float Lat2Meter = 111319.491f; private float getDistanceFromLocation(LocationInfo curr, LocationInfo prev) { Vector3 cv = new Vector3((float)curr.longitude, 0, (float)curr.latitude); Vector3 pv = new Vector3((float)prev.longitude, 0, (float)prev.latitude); returnVector3.Distance(cv, pv) * Lat2Meter; }

Place cursor at current position

Place the cursor on the map and rotate it to face the direction you are facing

this.transform.localEulerAngles = new Vector3(0, 0, 360 - Input.compass.trueHeading);

That's all

The finished product looks like this

summary

This ended up being a bit simple, but I'll wrap it up here for now. These
are my general thoughts.
・The Google Map API has a wide range of functions, so it would be interesting to try playing around with it.
・GPS errors are quite large.
・If it's just this much, the code can be made quite simple.

Now that we have AR and maps (GPS), let's try combining them to create something next time.
That's all for today.

I'll upload it to GitHub.
Unity Sample

lastly

I am a member of the system development service site "SEKARAKU Lab."
Beyond offers a one-stop service for everything from server design and construction to operation, so if you have any problems with server-side development, please feel free to contact us.
SEKARAKU Lab: [https://sekarakulab.beyondjapan.com/](https://sekarakulab.beyondjapan.com/)

If you found this article useful, please click [Like]!
1
Loading...
1 vote, average: 1.00 / 11
8,825
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Matsuyama Kensho

He has long worked in programming and project management at a game development company.
In 2019, he joined Beyond Inc. and works in the Yokohama office.
He is primarily responsible for project management of server-side development work (occasionally programming).
His hobbies are cycling (road racing) and watching horse racing.