Unity 定位

老样子 只说操作

定位的方法:1. 获取经纬度  可以用Unity本身的Input.location。(弊端:性能不强 ,部分手机获取不到,信号不好时获取不到,手机太老获取不到,始终显示状态为:LocationServiceStatus.Initializing)

方法二: 接SDK  (我这里接的是高德定位)

 先说方法一:

创建脚本:

using UnityEngine;
using System.Collections;

public class GetGPS : MonoBehaviour
{
    public string gps_info = "";
    public int flash_num = 1;

    // Use this for initialization  
    void Start()
    {

    }

    void OnGUI()
    {
        GUI.skin.label.fontSize = 28;
        GUI.Label(new Rect(20, 20, 600, 300), this.gps_info);
        GUI.Label(new Rect(20, 350, 600, 48), this.flash_num.ToString());

        GUI.skin.button.fontSize = 50;
        if (GUI.Button(new Rect(Screen.width / 2 - 110, 200, 220, 85), "GPS定位"))
        {
            StartGPS();
        }
        if (GUI.Button(new Rect(Screen.width / 2 - 110, 500, 220, 85), "刷新GPS"))
        {
            this.gps_info = "  刷新GPSN:" + Input.location.lastData.latitude + "    刷新GPSE:" + Input.location.lastData.longitude;
            this.gps_info +="   状态:" + Input.location.status;
            this.flash_num += 1;
        }
    }

    //开启定位
    void StartGPS()
    {

        if (!Input.location.isEnabledByUser)
        {
            this.gps_info += "定位没有开启:" + Input.location.isEnabledByUser.ToString();
        }
        else
        {
            Input.location.Start(10.0f, 10.0f);
            this.gps_info += "正在开启定位Start:";

        }
    }
    //获取定位
    void GetUnityGPS()
    {
        if (Input.location.status == LocationServiceStatus.Stopped)
        {
            this.gps_info="定位服务已经停止";
        }
        else if (Input.location.status == LocationServiceStatus.Initializing)
        {
            this.gps_info = " 定位服务正在初始化,在一段时间后,状态会切换回来";
        }
        else if (Input.location.status == LocationServiceStatus.Running)
        {
            this.gps_info = "成功定位";
        }
        else if (Input.location.status == LocationServiceStatus.Failed)
        {
            this.gps_info = "位置服务失败(用户拒绝访问位置服务)";
        }
    }
}
方法二:SDK定位

1.获取key (官网上有,关于在发布正式版本的时候要把自己Unity生成的签名key放在C:\Users\Administrator\.android下,然后用控制台的方式输出你的key)

2.获取Jar包(下载高德的一键下载包里 有一个AMapLocation文件 里面只有一个jar包,在eclips编译的时候用(我这里只说的是定位))

3.参考http://blog.csdn.net/u011192809/article/details/52355125?locationNum=3(里面已经说的很详细了可以获取的很全面)

我留了两个测试apk和完整Unity项目



猜你喜欢

转载自blog.csdn.net/m0_37583098/article/details/78465791