Try using GPS and Google Map API with Unity
thank you for your hard work.
My name is Matsuyama and I am from the System Development Department at the Yokohama Office.
It's been a while since last time.
Moving away from AR for a while, this time I will write about her GPS function and Google Map.
For now, let's try making something like this.
What to make
・Obtain your current location using GPS
・Display a map image centered on that point
・Update the map if you are moving at regular intervals
・Display an icon at your current location on the map
Obtaining location information
First, location information can be obtained using the LocationService class.
Reference LocationService
First, start updating the location.
Also, be sure to enable the compass function.
Reference Compass
Input.location.Start(); Input.compass.enabled = true;
Information cannot be obtained until it is started, so 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 is nothing you can do.
By the way, please note that if you do not set 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 in lastData of LocationService.
LocationInfo curr = Input.location.lastData;
Once you have obtained your current location (latitude and longitude), obtain a map image from those coordinates.
Map display
To use Google Maps, you need to register with Google Maps Platform in advance. (Explanation omitted)
Google Maps Platform Official
If you want to display a simple map image, you can obtain it using the Google Static Map API.
Acquires an image centered on the current location acquired by GPS.
① Specify parameters in URL
// Base URL string url = @"https://maps.googleapis.com/maps/api/staticmap?"; // Center coordinate url += "center=" + curr.latitude + "," + curr.longitude ; // Zoom url += "&zoom=" + 18; // 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 map image with UnityWebRequest
url = UnityWebRequest.UnEscapeURL(url); UnityWebRequest req = UnityWebRequestTexture.GetTexture(url); yield return req.SendWebRequest();
③ Display the downloaded image as a sprite
// Texture generation Texture2D tex = new Texture2D(MapSpriteSize, MapSpriteSize); tex.LoadImage(data); // Dynamically generate sprite (instance) mapImage.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
Let
's use a coroutine to update this process at regular intervals.
There is no need to constantly update the map, so update the map only when you have moved a certain amount or more from the previous coordinates.
The distance was calculated like this.
///<summary> /// Distance of 1 degree of latitude (longitude) (m) ///</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 your cursor on the map and rotate it in the direction you are facing.
this.transform.localEulerAngles = new Vector3(0, 0, 360 - Input.compass.trueHeading);
This is it.
The finished product looks like this.
summary
This is a bit of a simple topic, but I'll end this article here.
This is my rough impression.
・Google Map API has a lot of functions, so it will be interesting to play around with it.
・The error of GPS is relatively large.
・If this is the case, the code can be made quite simple.
Since we have AR and map (GPS), let's try combining them to create something next time.
Well, that's all for now.
I'll upload it to GitHub just in case.
Unity sample
lastly
I have opened the system development service site "SEKARAKU Lab" to which I belong.
Beyond is a one-stop service for everything from server design and construction to operation, so if you have any trouble with server-side development, please feel free to contact us.
SEKARAKU Lab: [https://sekarakulab.beyondjapan.com/](https://sekarakulab.beyondjapan.com/)