Topic: Google maps API Key

Hello,

I am currently using the plugin and have created an online map using google maps as a provider. I have seen that even though in the key manager I have not input any keys for google maps, the map itself is working fine both in the editor and in the mobile devices both ios and android.

Is there something I am missing? How can the maps work without a key? I have uploaded the app on closed testing for now and it seems to be working there as well. Will the maps cease to function when the app goes live?

Thank you.

Re: Google maps API Key

Hello.

This is the key for Google Web Services API (such as Geocoding API, Directions API, etc.), not tiles.
URL to the tiles, which is used in the preset for test purposes, does not use any key.

As written in Licenses and API keys section of the asset documentation:

The legal way is to use Google Maps tiles in your applications:
https://developers.google.com/maps/documentation/tile/

The test preset will work when the application goes live.
Use it, or the legal way, or change the tile provider, it's your choice.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Google maps API Key

So, even though it's fine to use it like that, to be 100% legal and covered I should get a key and just add it to the key manager at the google map field? No more changes are needed correct?

Thanks for all your help and congrats on the amazing plugin you've created!

Re: Google maps API Key

First of all, you need to access the Tiles API.
When and if it's ready, you can use it this way:

using System.Collections;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;

namespace InfinityCode.OnlineMapsExamples
{
    public class GoogleTilesAPI : MonoBehaviour
    {
        public string apiKey;
        public string mapType = "roadmap";
        public string language = "ja-JP";
        public string region = "region";
        public string[] layerTypes = { "layerRoadmap" };
        public bool overlay = false;
        public string scale = "scaleFactor1x";

        private string url = "https://www.googleapis.com/tile/v1/tiles/{zoom}/{x}/{y}?session={sessiontoken}&key={apikey}";
        private string sessiontoken;

        private IEnumerator GetSessionToken()
        {
            OnlineMapsJSONObject jReq = new OnlineMapsJSONObject();
            jReq.Add("mapType", mapType);
            jReq.Add("language", language);
            jReq.Add("region", region);
            jReq.Add("overlay", overlay);
            jReq.Add("scale", scale);
            OnlineMapsJSONArray jLayers = new OnlineMapsJSONArray();
            foreach (string l in layerTypes)
            {
                jLayers.Add(new OnlineMapsJSONValue(l));
            }

            jReq.Add("layerTypes", jLayers);

            UnityWebRequest www = UnityWebRequest.Post("https://www.googleapis.com/tile/v1/createSession?key=" + apiKey, jReq.ToString());
            yield return www.SendWebRequest();

            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.Log(www.error + "\n" + Encoding.UTF8.GetString(www.downloadHandler.data));
                yield break;
            }

            string response = Encoding.UTF8.GetString(www.downloadHandler.data);
            OnlineMapsJSONItem json = OnlineMapsJSON.Parse(response);
            sessiontoken = json.V<string>("session");

            OnlineMaps.instance.tileManager.tiles.ForEach(t =>
            {
                if (t.status == OnlineMapsTileStatus.error) t.status = OnlineMapsTileStatus.none;
            });

            OnlineMaps.instance.Redraw();
        }

        private string OnReplaceUrlToken(OnlineMapsTile tile, string token)
        {
            if (token == "sessiontoken") return sessiontoken;
            if (token == "apikey") return apiKey;
            return null;
        }

        private void OnStartDownloadTile(OnlineMapsTile tile)
        {
            if (string.IsNullOrEmpty(sessiontoken))
            {
                tile.status = OnlineMapsTileStatus.error;
                return;
            }

            OnlineMapsTileManager.StartDownloadTile(tile);
        }

        private void Start()
        {
            OnlineMapsProvider.GetProviders().FirstOrDefault(p => p.id == "google").AppendTypes(
                new OnlineMapsProvider.MapType("tilesapi") { urlWithLabels = url, }
            );

            OnlineMapsTile.OnReplaceURLToken += OnReplaceUrlToken;
            OnlineMapsTileManager.OnStartDownloadTile += OnStartDownloadTile;

            OnlineMaps.instance.mapType = "google.tilesapi";

            StartCoroutine(GetSessionToken());
        }
    }
}
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.