Advertisement
NovusX

static google map

Jul 27th, 2021
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.Networking;
  7.  
  8.  
  9. public class LocationPanel : MonoBehaviour, IPanel
  10. {
  11.     [SerializeField] RawImage _mapImage;
  12.     [SerializeField] GameObject _warningMessage;
  13.    
  14.     [Header("Map Variables")]
  15.     [SerializeField] string _apiKey;
  16.     [SerializeField] float _xCord, _yCord;
  17.     [SerializeField] int _zoom;
  18.     [SerializeField] int _imageSize;
  19.  
  20.     private static string _url = "https://maps.googleapis.com/maps/api/staticmap?";
  21.  
  22.     private IEnumerator Start()
  23.     {
  24.         #region Get Geo Coordinates
  25.         if (Input.location.isEnabledByUser)
  26.         {
  27.             Input.location.Start();
  28.  
  29.             var maxWaitTime = 20f;
  30.  
  31.             // Wait until service initializes
  32.             while (Input.location.status == LocationServiceStatus.Initializing && maxWaitTime > 0)
  33.             {
  34.                 yield return new WaitForSeconds(1f);
  35.                 maxWaitTime--;
  36.             }
  37.  
  38.             // Service didn't initialize in 20 seconds
  39.             if (maxWaitTime < 1f)
  40.             {
  41.                 StartCoroutine(WarningMessage("Location services failed to initialize, please try again."));
  42.             }
  43.  
  44.             // Connection has failed
  45.             if(Input.location.status == LocationServiceStatus.Failed)
  46.             {
  47.                 StartCoroutine(WarningMessage("Connection failed, please check your connection"));
  48.             }
  49.             else
  50.             {
  51.                 // Access granted and location value could be retrieved
  52.                 _xCord = Input.location.lastData.latitude;
  53.                 _yCord = Input.location.lastData.longitude;
  54.             }
  55.  
  56.             //stop the service after getting the coordinates
  57.             Input.location.Stop();
  58.         }
  59.         else
  60.         {
  61.             StartCoroutine(WarningMessage("Please enable location services."));
  62.             #if UNITY_EDITOR
  63.             _xCord = -23.5266189f;
  64.             _yCord = -46.6098616f;
  65.             #endif
  66.         }
  67.  
  68.         #endregion
  69.  
  70.         _url = _url + "center=" + _xCord + "," + _yCord + "&zoom=" + _zoom + "&size=" + _imageSize + "x" +
  71.                _imageSize + "&markers=color:red%7Clabel:P%7C" + _xCord + "," + _yCord + "&key=" + _apiKey;
  72.  
  73.  
  74.         StartCoroutine(GetMapRoutine(_url, (UnityWebRequest req) =>
  75.         {
  76.             if (req.result == UnityWebRequest.Result.ConnectionError || req.result == UnityWebRequest.Result.ProtocolError)
  77.             {
  78.                 Debug.LogError("Map error " + req.downloadHandler.text);
  79.             }
  80.             else
  81.             {
  82.                 var mapTexture = DownloadHandlerTexture.GetContent(req);
  83.                 _mapImage.texture = mapTexture;
  84.             }
  85.         }));
  86.  
  87.     }
  88.  
  89.         IEnumerator GetMapRoutine(string url, Action<UnityWebRequest> callback)
  90.         {
  91.             using var map = UnityWebRequestTexture.GetTexture(url);
  92.             yield return map.SendWebRequest();
  93.             callback(map);
  94.         }
  95.  
  96.     IEnumerator WarningMessage(string warning)
  97.     {
  98.         yield return new WaitForSeconds(0.5f);
  99.         _warningMessage.SetActive(true);
  100.         var message = _warningMessage.GetComponentInChildren<Text>();
  101.         message.text = warning;
  102.         yield return new WaitForSeconds(2f);
  103.         _warningMessage.SetActive(false);
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement