[TH] การใช้ GPS ใน Unity3D

จากบทความ Unity3D ครั้งที่แล้วได้กล่าวถึงฟีเจอร์บางส่วนในโปรแกรม ในบทความนี้จึงขอกล่าวฟีเจอร์หนึ่งที่น่าสนใจ คือการใช้งาน GPS ใน Unity3D โดยเฉพาะผู้เริ่มต้นใหม่ เนื่องจากในโปรแกรมมีการใช้งาน GPS ที่ค่อนข้างง่าย

ภาษาที่ใช้ในการเขียนโปรแกรมควบคุมในโปรแกรมคือภาษา C# ซึ่งเป็นภาษาเชิงวัตถุ ดังนั้นจึงต้องเขียนในลักษณะคลาส โดยโปรแกรมได้เตรียมคลาสที่จำเป็นไว้ให้ใช้งานเรียบร้อยแล้วคือคลาส MonoBehaviour นั่นเองครับ ซึ่งเราสามารถสืบทอดคุณสมบัติมาใช้งานได้ทันทีดังไฟล์ GPS.cs

GPS.cs

เป็นการตรวจสอบสถานการณ์เปิด GPS โดยหากผู้ใช้ไม่มีการเปิด GPS ไว้ โปรแกรมจะทำการแจ้งเตือนให้ผู้ใช้เปิด GPS และรออีก 20 วินาทีหากยังไม่มีสัญญาณ GPS โปรแกรมจะแจ้งเตือนและหยุดการค้นหา

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GPS : MonoBehaviour {
    public float latitude, longitude;
    public Text status;
    public static GPS Instance{set; get;}
    public bool succ = false;
    public bool scene2 = false;

	void Start () {
        Instance = this;
        DontDestroyOnLoad(gameObject);
        StartCoroutine(StartLocationService());
	}
	
    private IEnumerator StartLocationService()
    {
        
            if (!Input.location.isEnabledByUser)
            {
                status.text = "Please enable GPS";
                yield break;
            }
            Input.location.Start();
            int maxWait = 20;
            while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
            {
                yield return new WaitForSeconds(1);
                maxWait--;
            }
            if (maxWait <= 0)
            {
                status.text = "Time out";
                Debug.Log("Time out");
                yield break;
            }
            if (Input.location.status == LocationServiceStatus.Failed)
            {
                status.text = "can't determine device location";
                yield break;
            }        
        succ = true;
        scene2 = true;
        yield break;
    }
	void Update () {
        if (succ)
        {
            latitude = Input.location.lastData.latitude;
            longitude = Input.location.lastData.longitude;
        }
    }
}

หลังจากตรวจสอบสัญญาณ GPS เรียบร้อยแล้วเราสามารถเรียกตำแหน่งละติจูดและลองติจูดมาใช้งานได้ โดยในตัวอย่าง GPSCheck.cs เป็นการตรวจสอบตำแหน่งของผู้ใช้ว่าอยู่ในตำแหน่งที่กำหนดหรือไม่

GPSCheck.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;
using UnityEngine.SceneManagement;
public class GPSCheck : MonoBehaviour {

	private double[] t1Lat1 = { 12.073144, 12.073072, 12.073142, 12.073066 },
        t1Long1 = { 98.977573, 98.977581, 98.977794, 98.97794 },
        t1Lat2 = { 12.073142, 12.073066, 12.073152, 12.073070 },
        t1Long2 = { 98.977794, 98.977794, 98.978003, 98.978015 },
        t1Lat3 = { 12.073152, 12.073070, 12.073150, 12.073056 },
        t1Long3 = { 98.978003, 98.978015, 98.978198, 98.978208 };
		
    public float lat, lon;
    private float minLat = 9999999.00f, maxLat = -1.0f, minLong = 9999999.00f, maxLong = -1.0f;
	
    void Start () {
        minLat = (float)pbruLat.Min();
        maxLat = (float)pbruLat.Max();
        minLong = (float)pbruLong.Min();
        maxLong = (float)pbruLong.Max();
    }
	
	void Update () {
		lat = GPS.Instance.latitude;
        lon = GPS.Instance.longitude;
		
		if ((lat <= t1Lat1.Max() && lat >= t1Lat1.Min() && lon <= t1Long1.Max() && lon >= t1Long1.Min()) ||
                (lat <= t1Lat2.Max() && lat >= t1Lat2.Min() && lon <= t1Long2.Max() && lon >= t1Long2.Min()) ||
                (lat <= t1Lat3.Max() && lat >= t1Lat3.Min() && lon <= t1Long3.Max() && lon >= t1Long3.Min()))
            {
				//do something
			}
	}
}

การตรวจสอบตำแหน่งนั้นต้องใช้ทั้งละติจูดและลองติจูดซึ่งทำให้ต้องมีการเปรียบเทียบค่าหลายครั้ง โดยตรวจสอบว่าตัวแปร lat และ lon นั้นมีค่าอยู่ระหว่างค่าน้อยสุดและมากที่สุดของแต่ละชุดหรือไม่ โดยการตรวจสอบแบบนี้สามารถทำได้กับพื้นที่สี่เหลี่ยมเท่านั้น

สรุป

ในบทความนี้เป็นเพียงตัวอย่างเล็กน้อยในการใช้งาน GPS ใน Unity3D โดยการสืบทอดคุณสมบัติจากคลาส MonoBehaviour ที่มีพร้อมให้อยู่แล้ว ก่อนจะใช้งาน GPS จำเป็นต้องตรวจสอบสัญญาณว่าพร้อมใช้งานหรือไม่เพื่อลดข้อผิดพลาด พวกเราหวังว่าจะเป็นประโยชน์ต่อทุกท่านไม่มากก็น้อยและขอให้สนุกกับการเขียนโปรแกรมครับ

ท่านใดต้องการพูดคุยสามารถคอมเมนท์ได้เลยครับ

(C) 2020, โดย อ.จารุต บุศราทิจ/อ.ดนัย เจษฎาฐิติกุล
ปรับปรุงเมื่อ 2022-01-01