Files
tianrunCRM/Assets/3rd/LBSBaidu/Scripts/MyLocation.cs
2020-07-10 18:50:21 +08:00

131 lines
3.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Coolape;
using UnityEngine.Android;
using System.Runtime.InteropServices;
/// <summary>
/// My location.通过百度定位取得定位信息
/// </summary>
public class MyLocation : MonoBehaviour
{
public static MyLocation self;
public object callback;
public string ak4Ios = "B6gC5ufYoGSkM83NUA3qUzebhNT2hmWj"; //B6gC5ufYoGSkM83NUA3qUzebhNT2hmWj
public MyLocation()
{
self = this;
}
public int coorType = 0;
public class CoorType
{
public const int BD09ll = 0; // 百度经纬度坐标
public const int BD09 = 1; // 百度墨卡托坐标
public const int WGS84 = 2; //
public const int GCJ02 = 3; //国测局坐标
public const string BD09llName = "bd09ll"; // 百度经纬度坐标
public const string BD09Name = "bd09"; // 百度墨卡托坐标
public const string WGS84Name = "wgs84"; //
public const string GCJ0Name = "gcj02"; //国测局坐标
public static string getCoorTypeName(int type)
{
switch (type)
{
case 0:
return BD09llName;
case 1:
return BD09Name;
case 2:
return WGS84Name;
default:
return GCJ0Name;
}
}
}
#if UNITY_IOS
[System.Runtime.InteropServices.DllImport("__Internal")]
private static extern void _init(string ak, string goName, int coorType);
[System.Runtime.InteropServices.DllImport("__Internal")]
private static extern void _getMyLocation();
#endif
#if UNITY_ANDROID
static AndroidJavaClass _androidJavaClass;
public static AndroidJavaClass androidJavaClass
{
get
{
if (_androidJavaClass == null)
_androidJavaClass = new AndroidJavaClass("com.tianrun.BaiduLBS");
return _androidJavaClass;
}
}
#endif
public void init(int coorType)
{
this.coorType = coorType;
#if UNITY_ANDROID
androidJavaClass.CallStatic("init", gameObject.name, CoorType.getCoorTypeName(coorType));
#elif UNITY_IOS && !UNITY_EDITOR
_init(ak4Ios, gameObject.name, coorType);
#endif
}
public void guidSwitchGps()
{
#if UNITY_ANDROID
androidJavaClass.CallStatic("guidSwitchGps");
#endif
}
string[] permissions = new string[]
{
"android.permission.ACCESS_FINE_LOCATION",
"android.permission.ACCESS_COARSE_LOCATION",
"android.permission.INTERNET",
"android.permission.READ_PHONE_STATE",
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE",
"android.permission.ACCESS_NETWORK_STATE",
"android.permission.CHANGE_WIFI_STATE",
};
public void checkUserPermission()
{
#if UNITY_ANDROID
for (int i = 0; i < permissions.Length; i++)
{
if (!Permission.HasUserAuthorizedPermission(permissions[i]))
{
Permission.RequestUserPermission(permissions[i]);
}
}
#endif
}
public void getMyLocation(object callback)
{
checkUserPermission();
this.callback = callback;
#if UNITY_ANDROID
androidJavaClass.CallStatic("getMyLocation", CoorType.getCoorTypeName(coorType));
#elif UNITY_IOS && !UNITY_EDITOR
_getMyLocation();
#endif
}
void onCallback(string json)
{
//Hashtable map = JSON.DecodeMap(json);
Utl.doCallback(callback, json);
}
}