unity开发 百度地图后台定位
下面展示 百度地图后台定位。using UnityEngine;using System.Collections;using UnityEngine.UI;using ThreeSpaceJoin;using System;using MR_LBS.Client.Unity3D;using LitJson;using UnityEngine.SceneManagement;//切换到地图界面,与A
·
下面展示 百度地图后台定位
。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using ThreeSpaceJoin;
using System;
using MR_LBS.Client.Unity3D;
using LitJson;
using UnityEngine.SceneManagement;
//切换到地图界面,与Android相互传递消息,后台定位
public class BaiduMapAPI : MonoBehaviour
{
public static BaiduMapAPI instance;
[HideInInspector]
//接收定位点的经纬度的信息
public string myLongitude, myLatitude;
//[HideInInspector]
//是否由地图场景切换来
//public int fromMap;
//接收百度地图的指北针
Vector3 heading;
[HideInInspector]
public long containerId;
//当前Activity的对象
//AndroidJavaObject currentActivity;
void Awake()
{
instance = this;
}
void Start()
{
if (Application.platform != RuntimePlatform.Android)
return;
//获取context
//AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
UnityActivity in this demo.
//currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
openGPSDialog();
}
public void DelayToMap()
{
StartCoroutine(DelayToMapIEnumerator());
}
IEnumerator DelayToMapIEnumerator()
{
yield return new WaitForSeconds(2);
StartLBSMapOnClick();
}
/// <summary>
///通过按钮切换到百度地图
/// </summary>
public void StartLBSMapOnClick()
{
StopAllCoroutines();
UnityPhotonClient unityPhotonClient = UnityPhotonClient.Instance;
//总功能界面,探险按钮
if (unityPhotonClient.fightInfo == "" || unityPhotonClient.fightInfo == "null")
{
//StartCoroutine(SliderShow());
//调用AndroidJavaObject.Call 参数1表示调用UnityActivity.JAVA类中的方法名称,参数2表示该方法传递过去的参数
using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
{
//Debug.LogError(unityPhotonClient.loginData.ToJson().ToString());
//调用Android插件中UnityTestActivity中StartActivity0方法,stringToEdit表示它的参数
jo.Call("LoginMainActivity", unityPhotonClient.loginData.ToJson().ToString());
}
}
}
//战斗结束,确定按钮
else {
#region 友盟的数据统计
//UMengMethod.CardIDSet(BattleController.instance.myMonsters);
#endregion
PlayerController playerCon = PlayerController.instance;
//LoginController logCon = LoginController.instance;
//EnemyController enemyCon = EnemyController.instance;
BattleController battleCno = BattleController.instance;
//MainView mView = MainView.instance;
Player opp = playerCon.Opponent;
//object[] data = new object[2];
//当局获得积分
//(int)battleCno.attackerScore - battleCno.defenseScore;
//次元门desc
//JsonClass.ResourceToJson();
//次元门战斗力
//battleCno.aggressivity;
//次元门掠夺后的剩余资产
//(int)battleCno.resourceCount - battleCno.battleCoin;
JsonData battleResults = new JsonData();
battleResults["result"] = battleCno.result.ToString();
battleResults["battleCoin"] = (long)battleCno.spaceEnergy;
battleResults["battleScore"] = (long)(battleCno.battleScores);
if (battleCno.aggressivity == 0) {
battleResults["noOne"] = 1;//占领
}
else
battleResults["noOne"] = 0;
//次元门有主人,并且战斗结束战斗力不为0
if (opp != null && opp.UserId != UnityPhotonClient.Instance.wildOppUid)
battleResults["battleResHostId"] = opp.UserId;
else
battleResults["battleResHostId"] = UnityPhotonClient.Instance.wildOppUid;
if (battleCno.time_EnterGame.ToString().Length == 13 && battleCno.time_Gameover.ToString().Length == 13) {
battleResults["battleDuration"] = (int)Math.Ceiling((float)((battleCno.time_Gameover - battleCno.time_EnterGame) / 60000));
}
else
battleResults["battleDuration"] = 0;
unityPhotonClient.loginData["battleResult"] = battleResults;
//StartCoroutine(SliderShow());
#region
//AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
//AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
//调用Android插件中UnityTestActivity中StartActivity0方法,stringToEdit表示它的参数
//exp milage
//jo.Call("StartMainsActivity", toJson);//tostMsg为安卓方法名
#endregion
//调用AndroidJavaObject.Call 参数1表示调用UnityActivity.JAVA类中的方法名称,参数2表示该方法传递过去的参数
//using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
//{
// using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
// {
// //调用Android插件中UnityTestActivity中StartActivity0方法,stringToEdit表示它的参数
// jo.Call("WarEnd", unityPhotonClient.loginData.ToJson().ToString(), containerId);
// }
//}
SceneManager.LoadScene("EnterAndroid");
}
}
//进入地图前进度条
IEnumerator SliderShow()
{
GameObject go;
go = MainView.instance.toMapSlider;
go.transform.parent.gameObject.SetActive(true);
Slider slider = go.GetComponent<Slider>();
slider.value = 0.9f;
yield return 0;
//while (slider.value < 1)
//{
// yield return new WaitForFixedUpdate();
// slider.value += 0.01f;
//}
}
// 当前我的位置 myLocation是一个经纬度,用空格分开,1秒钟调用一次
// onReceiveLocation: {"latitude":32.086939,"longitude":118.919104}
public void currentLocationToUnity(string json)
{
JsonData jsonLoc = JsonMapper.ToObject(json);
myLatitude = jsonLoc["latitude"].ToString();
myLongitude = jsonLoc["longitude"].ToString();
}
/// <summary>
/// Send Currents orientation to unity.
/// onSensorChanged: {"z":-1.916988730430603,"x":9.419827256351709E-4,"y":0.004167246166616678}
/// </summary>
/// <param name="json">Json.</param>
public void currentOrientationToUnity(string json)
{
JsonData jsonOri = JsonMapper.ToObject(json);
heading = new Vector3(float.Parse(jsonOri["x"].ToString()), float.Parse(jsonOri["y"].ToString()), float.Parse(jsonOri["z"].ToString()));
}
//从地图获取对战的容器
//public void GetContainer(long containerId, long opponentId)
public void GetContainer(string content)
{
JsonData deJson = JsonMapper.ToObject(content);
PlayerController playerCon = PlayerController.instance;
//Debug.LogError("Test");
containerId = long.Parse(ExceptionSolve.StringExc(deJson["containerId"].ToJson()));
long opponentId = long.Parse(ExceptionSolve.StringExc(deJson["opponentId"].ToJson()));
//Debug.LogError("containerId:" + containerId);
//Debug.LogError("opponentId: " + opponentId);
BattleController.instance.containerId = containerId;
//0 位置,1 上锁,2 解锁
Operations.ResourceUpdateStatus(playerCon.PlayerMe.UserId, containerId, 1);
Operations.GetResource(opponentId, containerId);
//Debug.LogError("Test 2");
}
/// <summary>
/// 弹出GPS提示框
/// </summary>
public void openGPSDialog()
{
using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
{
//调用Android插件中UnityTestActivity中StartActivity0方法,stringToEdit表示它的参数
jo.Call("openGPS");
}
}
//currentActivity.Call("openGPS");
}
/// <summary>
/// Position Conversion.
/// </summary>
public Vector3 PositionConversion(double targetLat, double targetLon)
{
//B代表纬度,L代表经度,H代表高程
Vector3 myPosBLH = new Vector3(float.Parse(myLatitude), float.Parse(myLongitude), 0);
Vector3 targetPosBLH = new Vector3((float)targetLat, (float)targetLon, myPosBLH.z);
WGS84Model wgs84ModelMe = new WGS84Model(myPosBLH.x, myPosBLH.y, myPosBLH.z);
//WGS84Model wgs84ModelTarget = new WGS84Model (targetPosBLH.x, targetPosBLH.y, targetPosBLH.z);
WGS84Model wgs84ModelX = new WGS84Model(myPosBLH.x, targetPosBLH.y, myPosBLH.z);
WGS84Model wgs84ModelZ = new WGS84Model(targetPosBLH.x, myPosBLH.y, myPosBLH.z);
double distanceX = Math.Sqrt(Math.Pow(wgs84ModelMe.X - wgs84ModelX.X, 2) + Math.Pow(wgs84ModelMe.Y - wgs84ModelX.Y, 2) + Math.Pow(wgs84ModelMe.Z - wgs84ModelX.Z, 2));
double distanceY = FloatToDouble.ToFixedDecimal(targetPosBLH.z) - FloatToDouble.ToFixedDecimal(myPosBLH.z);
double distanceZ = Math.Sqrt(Math.Pow(wgs84ModelMe.X - wgs84ModelZ.X, 2) + Math.Pow(wgs84ModelMe.Y - wgs84ModelZ.Y, 2) + Math.Pow(wgs84ModelMe.Z - wgs84ModelZ.Z, 2));
// double radian = FloatToDouble.ToFixedDecimal(Input.compass.trueHeading) * Math.PI / 180;
float trueHeading;
if (heading.z <= 0 && heading.z >= -Mathf.PI)
{
trueHeading = heading.z + 2 * Mathf.PI;
}
else
{
trueHeading = heading.z;
}
double radian = FloatToDouble.ToFixedDecimal(trueHeading);
MobileToWorldModel mobileToWorldModel;
if (targetPosBLH.y >= myPosBLH.y && targetPosBLH.x >= myPosBLH.x)
{
//Debug.Log (Input.gyro.attitude.eulerAngles.z);
mobileToWorldModel = new MobileToWorldModel(distanceX, distanceY, distanceZ, radian);
}
else if (targetPosBLH.y >= myPosBLH.y && targetPosBLH.x < myPosBLH.x)
{
//Debug.Log (Input.gyro.attitude.eulerAngles.z);
mobileToWorldModel = new MobileToWorldModel(distanceX, distanceY, -distanceZ, radian);
}
else if (targetPosBLH.y < myPosBLH.y && targetPosBLH.x >= myPosBLH.x)
{
//Debug.Log (Input.gyro.attitude.eulerAngles.z);
mobileToWorldModel = new MobileToWorldModel(-distanceX, distanceY, distanceZ, radian);
}
else
{
//Debug.Log (Input.gyro.attitude.eulerAngles.z);
mobileToWorldModel = new MobileToWorldModel(-distanceX, distanceY, -distanceZ, radian);
}
//double distance = mobileToWorldModel.Radius;
// return new Vector3((float)mobileToWorldModel.X, (float)mobileToWorldModel.Y, -(float)mobileToWorldModel.Z);
return new Vector3((float)mobileToWorldModel.X, (float)mobileToWorldModel.Y, (float)mobileToWorldModel.Z);
}
public Vector3 PositionConversion(double targetLat, double targetLon, out double distance)
{
//B代表纬度,L代表经度,H代表高程
Vector3 myPosBLH = new Vector3(float.Parse(myLatitude), float.Parse(myLongitude), 0);
Vector3 targetPosBLH = new Vector3((float)targetLat, (float)targetLon, myPosBLH.z);
WGS84Model wgs84ModelMe = new WGS84Model(myPosBLH.x, myPosBLH.y, myPosBLH.z);
//WGS84Model wgs84ModelTarget = new WGS84Model (targetPosBLH.x, targetPosBLH.y, targetPosBLH.z);
WGS84Model wgs84ModelX = new WGS84Model(myPosBLH.x, targetPosBLH.y, myPosBLH.z);
WGS84Model wgs84ModelZ = new WGS84Model(targetPosBLH.x, myPosBLH.y, myPosBLH.z);
double distanceX = Math.Sqrt(Math.Pow(wgs84ModelMe.X - wgs84ModelX.X, 2) + Math.Pow(wgs84ModelMe.Y - wgs84ModelX.Y, 2) + Math.Pow(wgs84ModelMe.Z - wgs84ModelX.Z, 2));
double distanceY = FloatToDouble.ToFixedDecimal(targetPosBLH.z) - FloatToDouble.ToFixedDecimal(myPosBLH.z);
double distanceZ = Math.Sqrt(Math.Pow(wgs84ModelMe.X - wgs84ModelZ.X, 2) + Math.Pow(wgs84ModelMe.Y - wgs84ModelZ.Y, 2) + Math.Pow(wgs84ModelMe.Z - wgs84ModelZ.Z, 2));
// double radian = FloatToDouble.ToFixedDecimal(Input.compass.trueHeading) * Math.PI / 180;
float trueHeading;
if (heading.z <= 0 && heading.z >= -Mathf.PI)
{
trueHeading = heading.z + 2 * Mathf.PI;
}
else
{
trueHeading = heading.z;
}
double radian = FloatToDouble.ToFixedDecimal(trueHeading);
MobileToWorldModel mobileToWorldModel;
if (targetPosBLH.y >= myPosBLH.y && targetPosBLH.x >= myPosBLH.x)
{
//Debug.Log (Input.gyro.attitude.eulerAngles.z);
mobileToWorldModel = new MobileToWorldModel(distanceX, distanceY, distanceZ, radian);
}
else if (targetPosBLH.y >= myPosBLH.y && targetPosBLH.x < myPosBLH.x)
{
//Debug.Log (Input.gyro.attitude.eulerAngles.z);
mobileToWorldModel = new MobileToWorldModel(distanceX, distanceY, -distanceZ, radian);
}
else if (targetPosBLH.y < myPosBLH.y && targetPosBLH.x >= myPosBLH.x)
{
//Debug.Log (Input.gyro.attitude.eulerAngles.z);
mobileToWorldModel = new MobileToWorldModel(-distanceX, distanceY, distanceZ, radian);
}
else
{
//Debug.Log (Input.gyro.attitude.eulerAngles.z);
mobileToWorldModel = new MobileToWorldModel(-distanceX, distanceY, -distanceZ, radian);
}
distance = mobileToWorldModel.Radius;
// return new Vector3((float)mobileToWorldModel.X, (float)mobileToWorldModel.Y, -(float)mobileToWorldModel.Z);
return new Vector3((float)mobileToWorldModel.X, (float)mobileToWorldModel.Y, (float)mobileToWorldModel.Z);
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)