質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

0回答

629閲覧

『UnityによるARゲーム開発』ーGoogle Places APIの設定に関するご相談

Ekito

総合スコア4

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2021/03/08 12:20

Micheal Lanham著・高橋憲一ほか訳『UnityによるARゲーム開発』(オライリー・ジャパン)という本の内容から質問があります。

この本に沿って、UnityにGoogleMapを読み込み、特定のタイプの場所にマーカーを立てようとしました。
(p200 7章-5 「Google Places APIサービスの設定」)
![イメージ説明

しかし、 APIキーを取得してUnityプロジェクト内に設定しましたが、マーカーが表示されません。
作者によるサンプル用完成ファイルをダウンロードしてみましたが、同じようにマーカーが表示されませんでした。
おそらく、この本が出版された2017年当時から、Google Places APIの仕様が変更されたのではないかと思います。

この本についてご存知の方で、マーカーを表示させるためにどのような対応を施せば良いか分かる方がいらっしゃいましたら、ご教授いただければ幸いです。どうぞよろしくお願いいたします。

使用PC:Mac OS Catalina
使用ソフト:Unity Version 2019
使用言語:C#


*関連があるか明確ではないですが、ゲームをエディター内で実行した際、コンソールで以下のようなエラーが出ました。

NullReferenceException: Object reference not set to an instance of an object

packt.FoodyGO.Services.GPSLocationService.Start () (at Assets/FoodyGo/Scripts/Services/GPSLocationService.cs:55)

ここで、エラーが出ているスクリプトは以下のものです。(当該箇所55行目にコメントを加えました)

【GoogleLocationService.cs】

C#

1using packt.FoodyGo.Utils; 2using packt.FoodyGO.Managers; 3using packt.FoodyGO.Mapping; 4using System.Collections; 5using UnityEngine; 6 7namespace packt.FoodyGO.Services 8{ 9 [AddComponentMenu("Services/GPSLocationService")] 10 public class GPSLocationService : Singleton<GPSLocationService> 11 { 12 //Redraw Event 13 public delegate void OnRedrawEvent(GameObject g); 14 public event OnRedrawEvent OnMapRedraw; 15 [Header("GPS Accuracy")] 16 public float DesiredAccuracyInMeters = 10f; 17 public float UpdateAccuracyInMeters = 10f; 18 19 [Header("Map Tile Parameters")] 20 public int MapTileScale; 21 public int MapTileSizePixels; 22 public int MapTileZoomLevel; 23 24 [Header("GPS Simulation Settings")] 25 public bool Simulating; 26 public MapLocation StartCoordinates; 27 public float Rate = 1f; 28 public Vector2[] SimulationOffsets; 29 private int simulationIndex; 30 31 [Header("Exposed for GPS Debugging Purposes Only")] 32 public bool IsServiceStarted; 33 public float Latitude; 34 public float Longitude; 35 public float Altitude; 36 public float Accuracy; 37 public double Timestamp; 38 public double PlayerTimestamp; 39 public MapLocation mapCenter; 40 public MapEnvelope mapEnvelope; 41 public Vector3 mapWorldCenter; 42 public Vector2 mapScale; 43 44 45 //initialize the object 46 void Start() 47 { 48 print("Starting GPSLocationService"); 49 50#if !UNITY_EDITOR 51 StartCoroutine(StartService()); 52 Simulating = false; 53#else 54 StartCoroutine(StartSimulationService()); 55 Latitude = StartCoordinates.Latitude; //エラー(NullReferenceException) 56 Longitude = StartCoordinates.Longitude; 57 Accuracy = 10; 58 Timestamp = 0; 59 CenterMap(); 60#endif 61 } 62 63 IEnumerator StartSimulationService() 64 { 65 while (Simulating) 66 { 67 IsServiceStarted = true; 68 69 if (simulationIndex++ >= SimulationOffsets.Length-1) 70 { 71 simulationIndex = 0; 72 } 73 74 Longitude += SimulationOffsets[simulationIndex].x; 75 Latitude += SimulationOffsets[simulationIndex].y; 76 77 PlayerTimestamp = Epoch.Now; 78 79 yield return new WaitForSeconds(Rate); 80 } 81 IsServiceStarted = false; 82 } 83 84 //StartService is a coroutine, to avoid blocking as the location service is started 85 IEnumerator StartService() 86 { 87 // First, check if user has location service enabled 88 if (!Input.location.isEnabledByUser) 89 { 90 print("location not enabled by user, exiting"); 91 yield break; 92 } 93 94 // Start service before querying location 95 Input.location.Start(DesiredAccuracyInMeters, UpdateAccuracyInMeters); 96 97 // Wait until service initializes 98 int maxWait = 20; 99 while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) 100 { 101 yield return new WaitForSeconds(1); 102 maxWait--; 103 } 104 105 // Service didn't initialize in 20 seconds 106 if (maxWait < 1) 107 { 108 print("Timed out"); 109 yield break; 110 } 111 112 // Connection has failed 113 if (Input.location.status == LocationServiceStatus.Failed) 114 { 115 print("Unable to determine device location."); 116 yield break; 117 } 118 else 119 { 120 //make sure simulation is disbaled 121 Simulating = false; 122 print("GSPLocationService started"); 123 // Access granted and location value could be retrieved 124 print("Location initialized at: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp); 125 CenterMap(); 126 IsServiceStarted = true; 127 } 128 129 130 } 131 132 //called once per frame 133 void Update() 134 { 135 if(Input.location.status == LocationServiceStatus.Running && IsServiceStarted) 136 { 137 //updates the public values that can be consumed by other game objects 138 Latitude = Input.location.lastData.latitude; 139 Longitude = Input.location.lastData.longitude; 140 Altitude = Input.location.lastData.altitude; 141 Accuracy = Input.location.lastData.horizontalAccuracy; 142 PlayerTimestamp = Input.location.lastData.timestamp; 143 MapLocation loc = new MapLocation(Input.location.lastData.longitude, Input.location.lastData.latitude); 144 if (mapEnvelope.Contains(loc) == false) 145 { 146 Timestamp = Input.location.lastData.timestamp; 147 CenterMap(); 148 } 149 } 150 else if (Simulating && IsServiceStarted) 151 { 152 MapLocation loc = new MapLocation(Longitude, Latitude); 153 if (mapEnvelope.Contains(loc) == false) 154 { 155 Timestamp = PlayerTimestamp; 156 CenterMap(); 157 } 158 } 159 } 160 161 public void MapRedrawn() 162 { 163 if(OnMapRedraw != null) 164 { 165 OnMapRedraw(this.gameObject); 166 } 167 } 168 169 private void CenterMap() 170 { 171 mapCenter.Latitude = Latitude; 172 mapCenter.Longitude = Longitude; 173 mapWorldCenter.x = GoogleMapUtils.LonToX(mapCenter.Longitude); 174 mapWorldCenter.y = GoogleMapUtils.LatToY(mapCenter.Latitude); 175 176 mapScale.x = GoogleMapUtils.CalculateScaleX(Latitude, MapTileSizePixels, MapTileScale, MapTileZoomLevel); 177 mapScale.y = GoogleMapUtils.CalculateScaleY(Longitude, MapTileSizePixels, MapTileScale, MapTileZoomLevel); 178 179 var lon1 = GoogleMapUtils.adjustLonByPixels(Longitude, -MapTileSizePixels/2, MapTileZoomLevel); 180 var lat1 = GoogleMapUtils.adjustLatByPixels(Latitude, MapTileSizePixels/2, MapTileZoomLevel); 181 182 var lon2 = GoogleMapUtils.adjustLonByPixels(Longitude, MapTileSizePixels/2, MapTileZoomLevel); 183 var lat2 = GoogleMapUtils.adjustLatByPixels(Latitude, -MapTileSizePixels/2, MapTileZoomLevel); 184 185 mapEnvelope = new MapEnvelope(lon1, lat1, lon2, lat2); 186 } 187 188 //called when the object is destroyed 189 void OnDestroy() 190 { 191 if (IsServiceStarted) 192 Input.location.Stop(); 193 } 194 195 public MapLocation Location 196 { 197 get 198 { 199 return new MapLocation(Longitude, Latitude); 200 } 201 } 202 } 203} 204

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問