GPS 위치 입력

Published: May 6, 2022 by BeatChoi

GPS 좌표 입력

GPS 위치 데이터를 받아 좌표로 표현하는 방법을 알아봅니다.

스크립트 작성

GPSLocation 이라는 스크립트를 생성하고 아래와 같이 작성합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GPSLocation : MonoBehaviour
{
    public Text GPSStatus;
    public Text latitudeValue;
    public Text longitudeValue;
    public Text altitudeValue;
    public Text horizontalAccuracyValue;
    public Text timestampValue;

    public GameObject obj;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(GPSLoc());
    }

    IEnumerator GPSLoc()
    {

#if UNITY_EDITOR
// No permission handling needed in Editor
#elif UNITY_ANDROID
        if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(UnityEngine.Android.Permission.CoarseLocation)) {
            UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.CoarseLocation);
        }
#endif
        if (!Input.location.isEnabledByUser)
        {
            GPSStatus.text = "Not Enabled";
            yield break;
        }

        Input.location.Start();

        int maxWait = 20;
        while(Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            yield return new WaitForSeconds(1);
            maxWait--;
        }
        if(maxWait < 1)
        {
            GPSStatus.text = "Time Out";
            yield break;
        }
        if(Input.location.status == LocationServiceStatus.Failed)
        {
            GPSStatus.text = "Unable to determine device location";
            yield break;
        }
        else
        {
            GPSStatus.text = "Running";
            InvokeRepeating("UpdateGPSData", 0.5f, 1f);
            //access granted
        }
    }

    private void UpdateGPSData()
    {
        if(Input.location.status == LocationServiceStatus.Running)
        {
            GPSStatus.text = "Running";
            latitudeValue.text = Input.location.lastData.latitude.ToString();
            longitudeValue.text = Input.location.lastData.longitude.ToString();
            altitudeValue.text = Input.location.lastData.altitude.ToString();
            horizontalAccuracyValue.text = Input.location.lastData.horizontalAccuracy.ToString();
            timestampValue.text = Input.location.lastData.timestamp.ToString();
        }
        else
        {

        }
    }
}

28 : 안드로이드 디바이스에서 위지 서비스 이용 퍼미션을 받기위함.
32 : 디바이스에 위치 서비스가 작동하는지 체크.
38 : 위치 서비스 갱신을 시작함.
41 - 55 : 위치서비스 이용 불가 상황 사이드 케이스.
58 : GPS 위치 데이터 갱신.
63 : 위도, 경도, 고도 등 데이터 입력.

유니티 공식 문서에서 제공하는 스크립트를 기반으로 작성되었습니다.
https://docs.unity3d.com/ScriptReference/LocationService.Start.html를 참고하면 되겠습니다.

유니티 에디터에서

계층구조창에서 UI Canvas와 Text를 생성하고 다음과 같이 배치합니다.
GPS 위치 데이터를 받아서 표시할 항목은 좌측 Text 에서 보여집니다.


<01. UI Text 배치>


계층구조창에서 빈 게임 오브젝트를 생성하고 이름을 GPSLocation으로 변경합니다.
해당 오브젝트에 GPSLocation.cs스크립트를 연결하여 인스턴스화 시켜줍니다.
인스턴스화된 GPSLocation 컴포넌트에 변수항목들을 연결해줍니다.


<02. Inspector>


빌드

모바일 디바이스에 빌드하여 현재 좌표가 입력되는지 확인합니다.

Latest Posts

콘텐츠 개발 기법 - URP 환경에서 Bloom 및 Volume 포함한 화면 캡처
콘텐츠 개발 기법 - URP 환경에서 Bloom 및 Volume 포함한 화면 캡처

화면 캡처 :: Screen Capture

일반적으로 URP환경에서 Bloom 등의 Volume 이펙트들이 함께 캡처되지 않는 현상이 일어납니다.
두가지 원인이 있는데 첫번째는 저장할 Texture 및 Texture2D의 크기가 작아 모든 텍스쳐를 저장할 수 없는 경우와
두번째는 Linear color space의 픽셀을 텍스쳐에 저장 할 수 없는 경우가 있습니다.
이번 포스팅에서는 URP 환경에서 해당 이펙트들을 함께 캡쳐하는 방법을 알아봅니다.

콘텐츠 개발 기법 - UI 안나오게 화면 캡처(Screen Capture without UI)
콘텐츠 개발 기법 - UI 안나오게 화면 캡처(Screen Capture without UI)

화면 캡처 :: Screen Capture

UI 없이 화면을 캡쳐하는 방법을 알아봅니다.
해당 방법을 통해 사진 앱에서 사진을 찍거나 게임 내에서 UI를 제거한 스크린샷을 구현할 수 있습니다.

유니티3D 에디터에서

스크립트 작성

LightshipAR SDK 활용하기 - LightshipAR VPS의 활용
LightshipAR SDK 활용하기 - LightshipAR VPS의 활용

LightshipAR SDK

이번 강좌에서는 Visual Positioning System (VPS) 기능을 활용하여 콘텐츠를 개발해 봅니다.
VPS는 GPS정보와 타겟 매핑 정보를 정합하여 해당 타겟을 인식하는 기능입니다.
따라서 객체나 환경의 변화 및 이동이 잦지 않은 타겟이 스캔 대상이 됩니다.
주로 동상, 건물 입구, 가로등 등 위치의 변경이 없는 타겟들이 좋은 타겟입니다.

해당 타겟들을 인식시키고 주변에 가상의 오브젝트들을 배치하고 해당 위치를 저장하는 방법을 알아봅니다.

공간 매핑

Niantic Wayfarer

공간을 인식시키기 위해서 먼저 공간을 매핑해야 합니다.
이 과정은 IOS 어플리케이션으로 진행을 할 수 있는데 아래 링크에서

  1. Testflight
  2. Wayfarer

어플리케이션을 다운로드 및 설치합니다.

링크 : https://testflight.apple.com/join/VXu1F2jf