0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Google Map Tiles API を使用してタイルを表示する

Last updated at Posted at 2024-01-12

緯度,経度およびZoomからOpenStreetMapのタイルを得る
ではURLのみでマップタイルを表示できましたが、
googleMapTilesAPIはURLのみではタイルを表示できないようです。

なお、現時点では

Map Tiles API は現在プレビュー版(pre-GA)です。一般提供前のプロダクトと機能では、サポートが制限されることがあります。また、一般提供前のプロダクトや機能の変更は、他の一般提供前のバージョンと互換性がない場合があります。pre-GA のサービスには、Google Maps Platform Service Specific Terms が適用されます。詳細については、リリース ステージの説明をご覧ください。

となっています。

まずは結果から
image.png

説明にあるように、

curl -X POST -d '{
  "mapType": "roadmap",
  "language": "en-US",
  "region": "US"
}' \
-H 'Content-Type: application/json' \
"https://tile.googleapis.com/v1/createSession?key=YOUR_API_KEY"

でセッションをリクエストすると

{
  "session": "AJVsH2yTmy--bfUBuiOr6pogD3Bac6aNwqiHdV151e-...  ...",
  "expiry": "1706288086",
  "tileWidth": 256,
  "imageFormat": "png",
  "tileHeight": 256
}

のような結果が返ってきます。この
AJVsH2yTmy--bfUBuiOr6pogD3Bac6aNwqiHdV151e-... ...
の部分を、

curl "https://tile.googleapis.com/v1/2dtiles/z/x/y?session=YOUR_SESSION_TOKEN&key=YOUR_API_KEY&orientation=0_or_90_or_180_or_270"

YOUR_SESSION_TOKENに入れてあげると、冒頭部で示した画像が返ってきます。
/z/x/yに表示したいタイル番号を手に入れるためには
緯度,経度およびZoomからOpenStreetMapのタイルを得る
が便利です(2回目)

コードはこちら
Google2DTilesTest.cs
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

namespace GoogleMapTilesTest
{
    public class Google2DTilesTest : MonoBehaviour
    {
        [SerializeField] RawImage image;
        [SerializeField] Vector3Int pos = new Vector3Int(909, 403, 10);
        private string sessionUrl = "https://tile.googleapis.com/v1/createSession";
        private string tileUrl = "https://tile.googleapis.com/v1/2dtiles/{2}/{0}/{1}";
        [SerializeField] private string apiKey = "[YOUR_API_KEY]";

        class Google2DTilesSession
        {
            public string session;
            public string expiry;
            public string imageFormat;
            public int tileWidth;
            public int tileHeight;
        }

        // Start is called before the first frame update
        async void Start()
        {
            string json = "{\"mapType\":\"roadmap\",\"language\":\"en-US\",\"region\":\"US\"}";
            byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
            UploadHandlerRaw uploadHandler = new UploadHandlerRaw(bodyRaw);
            DownloadHandler downloadHandler = new DownloadHandlerBuffer();
            UnityWebRequest request = new UnityWebRequest(sessionUrl + "?key=" + apiKey, UnityWebRequest.kHttpVerbPOST);
            request.SetRequestHeader("ContentType", "application/json");
            request.uploadHandler = uploadHandler;
            request.downloadHandler = downloadHandler;
            await request.SendWebRequest();
            string retJson = request.downloadHandler.text;
            Debug.Log(retJson);

            Google2DTilesSession sessionObj = JsonUtility.FromJson<Google2DTilesSession>(retJson);
            var sessionID = sessionObj.session;
            //Debug.Log(sessionID);
            request.Dispose();

            string uri = string.Format(tileUrl, pos.x, pos.y, pos.z) + string.Format("?session={0}&key={1}", sessionID, apiKey);
            //Debug.Log(uri);

            UnityWebRequest texRequest = UnityWebRequest.Get(uri);
            await texRequest.SendWebRequest();
            var res = texRequest.result;
            if (res == UnityWebRequest.Result.ConnectionError)
            {
                Debug.Log(texRequest.error);
            }
            else
            {
                Texture2D tex = new Texture2D(2, 2);
                tex.LoadImage(texRequest.downloadHandler.data);
                image.texture = tex;
            }
            texRequest.Dispose();
            Debug.Log("done:" + res);
        }

        // Update is called once per frame
        void Update()
        {

        }
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?