ar_tourism_flutter_unity/unity/VRProject2/Assets/zhl/zhlScripts/GPSManager.cs
2025-05-14 17:04:13 +08:00

148 lines
4.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using UnityEngine;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
using UnityEngine.Networking;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.Android;
using UnityEngine.SceneManagement;
public class GPSManager : MonoBehaviour
{
public Vector3 unityPosition;
public Text txt;
public Text a;
public double doubleLongitude;
public double doubleLatitude;
public int i = 0;
public int UsersParentCount;
public bool isTake = false;
//public Transform userParent;
public Transform user;
void Start()
{
if (!Permission.HasUserAuthorizedPermission(Permission.FineLocation))
{
Permission.RequestUserPermission(Permission.FineLocation);
Input.location.Start(3f, 5f); // 每5秒更新一次精度5米
}
if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
{
Permission.RequestUserPermission(Permission.Camera);
}
StartCoroutine(StartGPS());
}
IEnumerator StartGPS()
{
while (true)
{
txt.text += "开始获取GPS信息";
// 检查位置服务是否可用
if (!Input.location.isEnabledByUser)
{
Debug.Log("位置服务不可用");
txt.text += "位置服务不可用";
a.text += "位置服务不可用";
break;
}
// 查询位置之前先开启位置服务
txt.text += "启动位置服务";
Input.location.Start();
// 等待服务初始化
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
txt.text += Input.location.status.ToString() + ">>>" + maxWait.ToString();
yield return new WaitForSeconds(1);
maxWait--;
}
// 服务初始化超时 2
if (maxWait < 1)
{
a.text = "服务初始化超时";
Debug.Log("服务初始化超时");
}
// 连接失败
if (Input.location.status == LocationServiceStatus.Failed)
{
a.text += "无法确定设备位置";
Debug.Log("无法确定设备位置");
}
else
{
doubleLongitude = Input.location.lastData.longitude;
doubleLatitude = Input.location.lastData.latitude;
}
unityPosition = GisPointTo3DPoint.Instance.GetWorldPoint(doubleLongitude, doubleLatitude);
Debug.Log("unityPosition" + unityPosition);
user.DOMove(unityPosition, 1f);
yield return new WaitForSeconds(2f);
i++;
}
}
public void InputTrans()
{
if (GisPointTo3DPoint.Instance)
{
Vector3 unityPosition = GisPointTo3DPoint.Instance.GetWorldPoint(doubleLongitude, doubleLatitude);
//Debug.Log("unityPosition" + unityPosition);
user.DOMove(unityPosition, 1f);
}
}
public void Update()
{
if(Input.GetKeyDown(KeyCode.K))
{
unityPosition = GisPointTo3DPoint.Instance.GetWorldPoint(doubleLongitude, doubleLatitude);
}
if(doubleLongitude == 0 || doubleLatitude == 0)
{
}
a.text = (doubleLongitude + "||" + doubleLatitude + "这是第" + i + "次更新"+"位置"+unityPosition).ToString();
if (doubleLongitude > 117.008169 && doubleLongitude < 117.026142 && doubleLatitude > 36.650872 && doubleLatitude < 36.672647||
doubleLongitude > 117.0173 && doubleLongitude < 117.0373 && doubleLatitude > 36.66503 && doubleLatitude < 36.68503
)
{
a.text += "您在泉城广场";
if (SceneManager.GetActiveScene().name != "SampleScene")
SceneManager.LoadScene("SampleScene");
}
else
{
a.text += "您不在泉城广场";
if (SceneManager.GetActiveScene().name != "OtherScene"){}
//SceneManager.LoadScene("OtherScene");
//执行不在泉城广场的逻辑
}
InputTrans();
}
string key = "83344857d1a4e2785b4e909fd00024c1";
}