본문 바로가기
Unity/Unity_API

[Unity] Static Google Map API 활용기

by Kyoung2 2022. 6. 28.
반응형

구글 API 를 사용하려면  구글 클라우드 플랫폼으로 접속하여야 한다.

구글 Maps api는 유료 서비스이며  200달러의 크레딧을 무료로 제공한다. 

200달러를 초과할 시 비용이 청구 될 수 있다.

 

https://mapsplatform.google.com/

 

Google Maps Platform - Location and Mapping Solutions

Create real world and real time experiences for your customers with dynamic maps, routes & places APIs from Google Maps Platform’s location solutions.

mapsplatform.google.com

시작하기를 누른다.

 

 

시작 후  API 키를 생성한다.

 

참고 : https://jeonhw.tistory.com/18

 

 

Static  맵을 호출하기위한 URL 정보

 

유니티 내에서 RawImage를 생성한다.

아래의  코드를 생성하고 

  public class MapManager : MonoBehaviour
    {
        public RawImage mapRawImage;

        [Header("맵 정보 설정")]
        public string strBaseURL = "";
        public double latitude = 35.000;
        public double longitude = 35.000;
        public int zoom = 14; 
        public int mapWidth;
        public int mapHeight;
        public string strAPIKey = "";

        // Start is called before the first frame update
        void Start()
        {
            mapRawImage = GetComponent<RawImage>();
            StartCoroutine(LoadMap());
        }

        IEnumerator LoadMap()
        {
            string url = strBaseURL + "center=" +latitude + "," + longitude +
                "&zoom=" + zoom.ToString() + "&size=" + mapWidth.ToString() + "x" + mapHeight.ToString()
                + "&key=" + strAPIKey;   //URL 생성  - 향후 StringBuilder를 이용해 적용.

            Debug.Log("URL : " + url);

            url = UnityWebRequest.UnEscapeURL(url); //Url에 대한  Web 요청
            UnityWebRequest req = UnityWebRequestTexture.GetTexture(url); //Texture에 대한 요청 
          
            yield return req.SendWebRequest();  //요청 전송

            mapRawImage.texture = DownloadHandlerTexture.GetContent(req); // 받은 Texture를 RAW 이미지에 적용
        }

   }

 

 

유니티 구글맵 API 연동

 

 

인스펙터 창에서 자신이 보여줄 맵의 위도와 경도를 설정하고 StrApiKey 에 자신의 키값을 넣으면 된다.

 

유니티 RawImage에 뜨는 연동 결과 창

728x90
반응형

# 로딩 화면 동작 코드(Code) 설정하기
loading