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

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

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

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

Q&A

解決済

1回答

787閲覧

GoogleMapTile.csのエラー

akaevaka

総合スコア8

C#

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

0グッド

0クリップ

投稿2021/11/05 12:57

###使用してる参考書
UnityによるARゲーム開発

###質問内容
tileの上にGoogle static mapから地図を持ってきて貼ろうとしているのですが,配布されるGoogleMapTile.csにおいてエラーが出てしまいました.
一旦この部分は問題なく通っていたのですが,本日projectを開いてみたところ,objectがmissingとなっているものがあり,その修正をしていたら,今までに出ていないエラーが出てしまったという流れです.

errorになってる関数が調べても出てこなくてどうすればいいのかわからなくなっています.

APIキーを貼らないと表示されないという問題はすでに解決済みです.

###エラー内容
NullReferenceException: Object reference not set to an instance of an object
packt.FoodyGO.Mapping.GoogleMapTile+<_RefreshMapTile>d__16.MoveNext () (at Assets/FoodyGo/Scripts/Mapping/GoogleMapTile.cs:132)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <d29d557b212f474390dc0b17ef6ce0dd>:0)

###問題となっている文

C#

1gpsLocationService.MapRedrawn();

###コード全体(APIキーは隠してあります)

C#

1using UnityEngine; 2using UnityEngine.Networking; 3using System.Collections; 4using packt.FoodyGO.Services; 5 6namespace packt.FoodyGO.Mapping 7{ 8 [AddComponentMenu("Mapping/GoogleMapTile")] 9 public class GoogleMapTile : MonoBehaviour 10 { 11 public enum MapType 12 { 13 RoadMap, 14 Satellite, 15 Terrain, 16 Hybrid 17 } 18 19 //Google Maps API Staticmap URL 20 private const string GOOGLE_MAPS_URL = "https://maps.googleapis.com/maps/api/staticmap"; 21 22 [Header("Map Settings")] 23 [Range(1,20)] 24 [Tooltip("Zoom Level, 1=global - 20=house")] 25 public int zoomLevel = 1; 26 [Tooltip("Type of map, Road, Satellite, Terrain or Hybrid")] 27 public MapType mapType = MapType.RoadMap; 28 [Range(64,1024)] 29 [Tooltip("Size in pixels of the map image")] 30 public int size = 640; 31 [Tooltip("Double the pixel resolution of the image returned")] 32 public bool doubleResolution = true; 33 [Tooltip("Defines the origin of the map")] 34 public MapLocation worldCenterLocation; 35 36 [Header("Tile Settings")] 37 [Tooltip("Sets the tiles position in tile units")] 38 public Vector2 TileOffset; 39 [Tooltip("Calculated tile center")] 40 public MapLocation tileCenterLocation; 41 [Tooltip("Calculated tile corners")] 42 public Vector2 TopLeftCorner; 43 public Vector2 BottomRightCorner; 44 45 [Header("GPS Settings")] 46 [Tooltip("GPS service used to locate world center")] 47 public GPSLocationService gpsLocationService; 48 private double lastGPSUpdate; 49 50 // Use this for initialization 51 void Start () 52 { 53 RefreshMapTile (); 54 } 55 56 // Update is called once per frame 57 void Update () 58 { 59 //check if a new location has been acquired 60 if (gpsLocationService != null && 61 gpsLocationService.IsServiceStarted && 62 lastGPSUpdate < gpsLocationService.Timestamp) 63 { 64 lastGPSUpdate = gpsLocationService.Timestamp; 65 worldCenterLocation.Latitude = gpsLocationService.Latitude; 66 worldCenterLocation.Longitude = gpsLocationService.Longitude; 67 print("GoogleMapTile refreshing map texture"); 68 RefreshMapTile(); 69 } 70 } 71 72 public void RefreshMapTile() { 73 74 StartCoroutine(_RefreshMapTile()); 75 } 76 77 IEnumerator _RefreshMapTile () 78 { 79 //find the center lat/long of the tile 80 tileCenterLocation.Latitude = GoogleMapUtils.adjustLatByPixels(worldCenterLocation.Latitude, (int)(size * 1 * TileOffset.y), zoomLevel); 81 tileCenterLocation.Longitude = GoogleMapUtils.adjustLonByPixels(worldCenterLocation.Longitude, (int)(size * 1 * TileOffset.x), zoomLevel); 82 83 var queryString = ""; 84 85 //build the query string parameters for the map tile request 86 queryString += "center=" + WWW.UnEscapeURL (string.Format ("{0},{1}", tileCenterLocation.Latitude, tileCenterLocation.Longitude)); 87 queryString += "&zoom=" + zoomLevel.ToString (); 88 queryString += "&size=" + WWW.UnEscapeURL (string.Format ("{0}x{0}", size)); 89 queryString += "&scale=" + (doubleResolution ? "2" : "1"); 90 queryString += "&maptype=" + mapType.ToString ().ToLower (); 91 queryString += "&format=" + "png"; 92 93 //adding the map styles 94 queryString += "&style=element:geometry|invert_lightness:true|weight:3.1|hue:0x00ffd5"; 95 queryString += "&style=element:labels|visibility:off"; 96 97 //queryString += "&key={your API key here}"; 98 99 //check if script is on a mobile device and using a location service s 100 var usingSensor = false; 101#if MOBILE_INPUT 102 usingSensor = Input.location.isEnabledByUser 103 && Input.location.status == LocationServiceStatus.Running 104 && gpsLocationService !=null; 105#endif 106 queryString += "&sensor=" + (usingSensor ? "true" : "false"); 107 108 //set map bounds rect 109 TopLeftCorner.x = GoogleMapUtils.adjustLonByPixels(tileCenterLocation.Longitude, -size, zoomLevel); 110 TopLeftCorner.y = GoogleMapUtils.adjustLatByPixels(tileCenterLocation.Latitude, size, zoomLevel); 111 112 BottomRightCorner.x = GoogleMapUtils.adjustLonByPixels(tileCenterLocation.Longitude, size, zoomLevel); 113 BottomRightCorner.y = GoogleMapUtils.adjustLatByPixels(tileCenterLocation.Latitude, -size, zoomLevel); 114 115 print(string.Format("Tile {0}x{1} requested with {2}", TileOffset.x, TileOffset.y, queryString)); 116 117 //finally, we request the image 118 var req = UnityWebRequestTexture.GetTexture(GOOGLE_MAPS_URL + "?" + queryString + "&key"); 119 //yield until the service responds 120 yield return req.Send(); 121 //first destroy the old texture first 122 Destroy(GetComponent<Renderer>().material.mainTexture); 123 //when the image returns set it as the tile texture 124 if (req.error != null) { 125 print (string.Format ("Error loading tile {0}x{1}: exception={2}", 126 TileOffset.x, TileOffset.y, req.error)); 127 } else { 128 //when the image returns set it as the tile texture 129 GetComponent<Renderer> ().material.mainTexture = ((DownloadHandlerTexture)req.downloadHandler).texture; 130 print (string.Format ("Tile {0}x{1} textured", TileOffset.x, TileOffset.y)); 131 if (TileOffset.x == 0 && TileOffset.y == 0) { 132 gpsLocationService.MapRedrawn(); 133 } 134 } 135 } 136 } 137} 138

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

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

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

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

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

guest

回答1

0

自己解決

TileにGPSを付けていないのが原因でした

投稿2021/11/06 11:57

akaevaka

総合スコア8

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問