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

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

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

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

Q&A

解決済

1回答

526閲覧

【Unity】Googlemapの検索画面のようなものを作りたい

kimkim

総合スコア142

Unity

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

0グッド

1クリップ

投稿2018/07/06 07:40

前提・実現したいこと

Unityでやることではないかもしれませんが、
住所や場所の名称を入力するとその候補を表示する機能を実現したいです。

イメージ説明

↑このような画面です。

発生している問題

とりあえず地図を表示するAPIなどを探してみたのですが
見たところそのような機能があるものはないように感じました。

もし、実現する方法があれば教えていただきたいです。
お願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

使うとしたらGoogle Maps Platformのプレイスでしょうかね?

下記のようなコードで実験してみたところ...

C#

1using System; 2using System.Collections; 3using System.Collections.Generic; 4using System.Linq; 5using System.Text; 6using UnityEngine; 7using UnityEngine.Networking; 8using UnityEngine.UI; 9 10public class PlaceFinder : MonoBehaviour 11{ 12 private const string ApiKey = /* APIキー */; 13 public InputField RequestField; // 画面左上のキーワード入力用フィールド 14 public Text ResultTextPrefab; // 結果表示ビューに表示するためのテキストのプレハブ 15 public RectTransform ResultView; // 画面中央の結果表示ビュー 16 private readonly List<GameObject> resultObjects = new List<GameObject>(); // 現在表示されている結果テキストのリスト 17 18 // 画面右上の検索ボタンのOnClickで実行するメソッド、senderには検索ボタン自身をセットしておく 19 public void OnClickFindButton(Button sender) 20 { 21 this.StartCoroutine(this.FindPlaces(sender)); 22 } 23 24 // 文字列中のrangesが示す範囲にcolorタグを仕込んで色づけするメソッド 25 private static string ColorizeString(string source, Color32 color, IEnumerable<MatchedSubstring> ranges) 26 { 27 var colorOpenTag = string.Format("<color=#{0:x2}{1:x2}{2:x2}>", color.r, color.g, color.b); 28 const string colorCloseTag = "</color>"; 29 var result = new StringBuilder(source); 30 var offset = 0; 31 foreach (var range in ranges.OrderBy(r => r.offset)) 32 { 33 offset += range.offset; 34 result.Insert(offset, colorOpenTag); 35 offset += colorOpenTag.Length + range.length; 36 result.Insert(offset, colorCloseTag); 37 offset += colorCloseTag.Length - (range.offset + range.length); 38 } 39 40 return result.ToString(); 41 } 42 43 private IEnumerator FindPlaces(Selectable sender) 44 { 45 using (var request = UnityWebRequest.Get( 46 new Uri( 47 string.Format( 48 "https://maps.googleapis.com/maps/api/place/autocomplete/json?input={0}&key={1}", 49 this.RequestField.text, 50 ApiKey)))) 51 { 52 // 検索ボタンを使用不能にする 53 sender.interactable = false; 54 55 // Google Mapsにリクエストを送信、結果が返ってくるまで待機 56 yield return request.SendWebRequest(); 57 58 // 検索ボタンを使用可能にする 59 sender.interactable = true; 60 61 // 既存の結果表示は削除、resultObjectsを空にする 62 foreach (var resultObject in this.resultObjects) 63 { 64 Destroy(resultObject); 65 } 66 this.resultObjects.Clear(); 67 68 if (request.isNetworkError || request.isHttpError) 69 { 70 Debug.Log(request.error); 71 } 72 else 73 { 74 // 受け取った結果をResponseオブジェクトに展開し、その内容に基づいて結果テキストオブジェクトを作る 75 var responseString = request.downloadHandler.text; 76 Debug.Log(responseString); 77 var response = JsonUtility.FromJson<Response>(responseString); 78 Debug.Log(response.status); 79 foreach (var prediction in response.predictions) 80 { 81 var resultString = prediction.description; 82 var result = Instantiate(this.ResultTextPrefab, this.ResultView); 83 result.text = ColorizeString(resultString, Color.red, prediction.matched_substrings); 84 this.resultObjects.Add(result.gameObject); 85 } 86 } 87 } 88 } 89 90 [Serializable] 91 public class Response 92 { 93 public Prediction[] predictions; 94 public string status; 95 } 96 97 [Serializable] 98 public class Prediction 99 { 100 public string description; 101 public MatchedSubstring[] matched_substrings; 102 public string place_id; 103 public Term[] terms; 104 public string[] types; 105 } 106 107 [Serializable] 108 public class MatchedSubstring 109 { 110 public int length; 111 public int offset; 112 } 113 114 [Serializable] 115 public class Term 116 { 117 public int offset; 118 public string value; 119 } 120}

このような結果になりました。

結果

参考1: プレイス オートコンプリート | Google Places API Web Service | Google Developers
参考2: Google Maps APIが新しくなる!Google Maps Platformの料金体系と必要な設定変更 | mariweb

投稿2018/07/06 21:06

Bongo

総合スコア10807

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

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

kimkim

2018/07/07 04:02

回答ありがとうございます! そしてわざわざここまで作ってくださりありがとうございます! 完全に自分の理想のものだと思います! 今は少し試せないのですが、3日後に試して見てからベストアンサーにさせていただきたいと思います!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問