using UnityEngine;
using System.Collections;
public class GetGPS : MonoBehaviour
{
? ? void StopGPS()
? ? {
? ? ? ? Input.location.Stop();
? ? }
? ? public void StartGetGPS()
? ? {
? ? ? ? StartCoroutine(StartGPS());
? ? }
? ? IEnumerator StartGPS()
? ? {
? ? ? ? // Input.location 用于訪問設(shè)備的位置屬性(手持設(shè)備), 靜態(tài)的LocationService位置?
? ? ? ? // LocationService.isEnabledByUser 用戶設(shè)置里的定位服務(wù)是否啟用?
? ? ? ? if (!Input.location.isEnabledByUser)
? ? ? ? {
? ? ? ? ? ?Debug.Log?("請開啟GPS權(quán)限毯炮!");
? ? ? ? }
? ? ? ? // LocationService.Start() 啟動位置服務(wù)的更新,最后一個位置坐標(biāo)會被使用?
? ? ? ? Input.location.Start(10.0f, 10.0f);
? ? ? ? int maxWait = 20;
? ? ? ? while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
? ? ? ? {
? ? ? ? ? ? // 暫停協(xié)同程序的執(zhí)行(1秒)?
? ? ? ? ? ? yield return new WaitForSeconds(1);
? ? ? ? ? ? maxWait--;
? ? ? ? }
? ? ? ? if (maxWait < 1)
? ? ? ? {
? ? ? ? ? ? Debug.Log("Init GPS service time out");? ? ? ? ?
? ? ? ? }
? ? ? ? if (Input.location.status == LocationServiceStatus.Failed)
? ? ? ? {
? ? ? ? ? ? Debug.Log("Unable to determine device location");? ? ? ? ?
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? SetPlayerPlace();
? ? ? ? ? ? yield return new WaitForSeconds(100);
? ? ? ? }
? ? }
? ? void SetPlayerPlace()
? ? {
? ? ? ? if (Input.location.lastData.longitude!=0)
? ? ? ? {
? Debug.Log("經(jīng)度:"+Input.location.lastData.longitude+"? 緯度:"+Input.location.lastData.latitude);
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? Debug.Log("獲取失敗!");
? ? ? ? }
? ? ? ? Input.location.Stop();
? ? }
//兩個經(jīng)緯度 算距離
private const double EARTH_RADIUS = 6371010;
? ? /// <summary>
? ? /// 計算兩點位置的距離推沸,返回兩點的距離比藻,單位:米
? ? /// 該公式為GOOGLE提供粹舵,誤差小于0.2米
? ? /// </summary>
? ? /// <param name="lng1">第一點經(jīng)度</param>
? ? /// <param name="lat1">第一點緯度</param>? ? ? ?
? ? /// <param name="lng2">第二點經(jīng)度</param>
? ? /// <param name="lat2">第二點緯度</param>
? ? /// 34.65 33.22? 18.99 17.11
? ? /// <returns></returns>
? ? public static string GetDistance(string[] pos1, string[] pos2)
? ? {
? ? ? ? if (pos1 == null || pos2 == null || pos1[0] == null || pos2[0] == null)
? ? ? ? {
? ? ? ? ? ? return "位置未知";
? ? ? ? }else{
double radLat1 = Rad(double.Parse(pos1[0]));
? ? ? ? ? ? double radLng1 = Rad(double.Parse(pos1[1]));
? ? ? ? ? ? double radLat2 = Rad(double.Parse(pos2[0]));
? ? ? ? ? ? double radLng2 = Rad(double.Parse(pos2[1]));
? ? ? ? ? ? double a = radLat1 - radLat2;
? ? ? ? ? ? double b = radLng1 - radLng2;
? ? ? ? ? ? double result = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))) * EARTH_RADIUS;
? ? ? ? ? ? if (result > 1000)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? double resultG = result / 1000;
? ? ? ? ? ? ? ? return Convert.ToDouble(resultG).ToString("0.00") + "公里";
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return Convert.ToDouble(result).ToString("0.00") + "米";
? ? ? ? ? ? }
}
? ? }
? ? /// <summary>
? ? /// 經(jīng)緯度轉(zhuǎn)化成弧度
? ? /// </summary>
? ? /// <param name="d"></param>
? ? /// <returns></returns>
? ? private static double Rad(double d)
? ? {
? ? ? ? return (double)d * Math.PI / 180d;
? ? }
}