public struct Longlatitude
{
public double Longitude;
public double latitude;
}
public class GPSManager
{
private const double EARTH_RADIUS = 6378137;
private static bool bStart=false;
private static Dictionary<ushort, Longlatitude> dic = new Dictionary<ushort, Longlatitude>();
public static Action eventGPS;
// public static Dictionary<ushort, double> distance = new Dictionary<ushort, double>();//key为0代表0距离1 key为2代表2距离0
public static IEnumerator StartGPS(Action<LocationInfo> CallBack) //初始GPS 获取位置
{
if (bStart)
{
yield break;
}
bStart = true;
// 检查位置服务是否可用
if (!Input.location.isEnabledByUser)
{
Debug.LogError("服务不可用");
bStart = false;
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)
{
Debug.LogError("服务初始化超时");
bStart = false;
yield break;
}
// 连接失败
if (Input.location.status == LocationServiceStatus.Failed)
{
Debug.LogError("连接失败");
bStart = false;
yield break;
}
else
{
CallBack(Input.location.lastData);
//string str = "Location: rn" +
//"纬度:" + Input.location.lastData.latitude + "rn" +
//"经度:" + Input.location.lastData.longitude + "rn" +
//"海拔:" + Input.location.lastData.altitude + "rn" +
//"水平精度:" + Input.location.lastData.horizontalAccuracy + "rn" +
//"垂直精度:" + Input.location.lastData.verticalAccuracy + "rn" +
//"时间戳:" + Input.location.lastData.timestamp;
}
// 停止服务,如果没必要继续更新位置,(为了省电)
Input.location.Stop();
bStart = false;
}
/// <summary>
/// 计算两点位置的距离,返回两点的距离,单位 米
/// 该公式为GOOGLE提供,误差小于0.2米
/// </summary>
/// <param name="lat1">第一点纬度</param>
/// <param name="lng1">第一点经度</param>
/// <param name="lat2">第二点纬度</param>
/// <param name="lng2">第二点经度</param>
/// <returns></returns>
public static double GetDistance(double lat1, double lng1, double lat2, double lng2) //第一玩家纬度,第一玩家经度,第二玩家纬度,第二玩家经度, //获取两个玩家的距离
{
double radLat1 = Rad(lat1);
double radLng1 = Rad(lng1);
double radLat2 = Rad(lat2);
double radLng2 = Rad(lng2);
double a = radLat1 - radLat2;
double b = radLng1 - radLng2;
double result = 2 * System.Math.Asin(System.Math.Sqrt(System.Math.Pow(System.Math.Sin(a / 2), 2) + System.Math.Cos(radLat1) * System.Math.Cos(radLat2) * System.Math.Pow(System.Math.Sin(b / 2), 2))) * EARTH_RADIUS;
return result;
}
/// <summary>
/// 经纬度转化成弧度
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
private static double Rad(double d)
{
return (double)d * System.Math.PI / 180d;
}
public static bool GetGPSDistance(ushort chairID_1,ushort chairID_2, out double dis) //第一个玩家,第二个玩家
{
dis = 0;
if (dic.ContainsKey(chairID_1)&& dic.ContainsKey(chairID_2))
{
dis = GetDistance(dic[chairID_1].latitude, dic[chairID_1].Longitude, dic[chairID_2].latitude, dic[chairID_2].Longitude);
return true;
}
else
{
return false;
}
}
public static void AddGPSInfo(ushort chairID,double Longitude, double latitude) //保存每个玩家的ID和经纬度
{
Longlatitude data = new Longlatitude();
data.Longitude = Longitude;
data.latitude = latitude;
if (dic.ContainsKey(chairID))
{
dic[chairID] = data;
}
else
{
dic.Add(chairID, data);
}
if (eventGPS != null)
{
eventGPS();
}
}
public static void RemoveAt(ushort chairID) //移除玩家
{
if (dic.ContainsKey(chairID))
{
dic.Remove(chairID);
}
}
public static void RemoveAll() //移除所有玩家
{
dic.Clear();
}
}
unity 自带定位
猜你喜欢
转载自blog.csdn.net/CADNzhu/article/details/81189523
今日推荐
周排行