Unity获取GPS经纬度并解析成具体位置

其实unity也有自带的api获取GPS经纬度的信息的,地址是:https://docs.unity3d.com/ScriptReference/LocationService.Start.html

下面写一个demo,把获取到的GPS信息的经纬度解析成具体地址,具体思路是通过unity 自带api获取经纬度,然后通过高德的webapi把经纬度转换成具体的地址,废话少说,看代码:

using System;
using UnityEngine;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
using LitJson;
using UnityEngine.Networking;
using UnityEngine.UI;

public class GPSManager : MonoBehaviour
{

    public Text txt;
    public Text AddressText;
    private string longitude;//经度
    private string latitude;//纬度

  

    void Start()
    {
        StartCoroutine(StartGPS());
    }
    IEnumerator StartGPS()
    {
        txt.text = "开始获取GPS信息";

        // 检查位置服务是否可用  
        if (!Input.location.isEnabledByUser)
        {
            txt.text = "位置服务不可用";
            yield break;
        }

        // 查询位置之前先开启位置服务  
        txt.text = "启动位置服务";
        Input.location.Start();

        // 等待服务初始化  
        int maxWait = 20;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            txt.text = Input.location.status.ToString() + ">>>" + maxWait.ToString();
            yield return new WaitForSeconds(1);
            maxWait--;
        }

        // 服务初始化超时  
        if (maxWait < 1)
        {
            txt.text = "服务初始化超时";
            yield break;
        }

        // 连接失败  
        if (Input.location.status == LocationServiceStatus.Failed)
        {
            txt.text = "无法确定设备位置";
            yield break;
        }
        else
        {
            txt.text = "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;

            longitude = Input.location.lastData.longitude.ToString();
            latitude = Input.location.lastData.latitude.ToString();

            StartCoroutine(GetRequest(
                "http://restapi.amap.com/v3/geocode/regeo?key=6bda73179a87a92394489045b32a0f46&location="+longitude+","+latitude));
        }

        // 停止服务,如果没必要继续更新位置,(为了省电)  
        Input.location.Stop();
    }

     string key = "6bda73179a87a92394489045b32a0f46";       //去高德地图开发者申请

    IEnumerator GetRequest(string uri)
    {
        using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
        {
            // Request and wait for the desired page.
            yield return webRequest.SendWebRequest();

            string[] pages = uri.Split('/');
            int page = pages.Length - 1;

            if (webRequest.isNetworkError)
            {
               // Debug.Log(pages[page] + ": Error: " + webRequest.error);
            }
            else
            {
               JsonData jd=JsonMapper.ToObject(webRequest.downloadHandler.text);
              AddressText.text=jd["regeocode"]["formatted_address"].ToString();
                // Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
            }
        }
    }
}

    

关于key,可以去高德开放平台注册成为开发者,创建应用即可

https://lbs.amap.com/

逆地理编码

看看我打包到手机的效果图:

字有点小,将就看吧,最后我把demo源码上传了,关注公众号,回复关键字“GPS”获取

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要根据经纬度更改Unity中的地形,你需要进行以下步骤: 1. 获取地形高度数据。你可以使用一些在线API或者自己创建高度图,将其导入到Unity中。 2. 将经纬度转换为Unity中的坐标。你可以使用一些库或者自己编写代码来实现。 3. 在地形上更改高度值。通过遍历地形上的每个点,并将其高度值更改为对应的高度数据,来实现更改地形高度。 以下是一个简单的示例代码,可以根据经纬度更改Unity中的地形高度: ```csharp public Terrain terrain; // 地形对象 public float[,] heightData; // 高度数据 public void ChangeTerrainHeight(float latitude, float longitude, float height) { int width = terrain.terrainData.heightmapResolution; int height = terrain.terrainData.heightmapResolution; float resolutionX = terrain.terrainData.size.x / (width - 1); float resolutionZ = terrain.terrainData.size.z / (height - 1); // 将经纬度转换为Unity坐标 float x = (longitude + 180) / 360 * width * resolutionX; float z = (latitude + 90) / 180 * height * resolutionZ; // 获取地形上的点的高度值 heightData = terrain.terrainData.GetHeights(0, 0, width, height); // 更改高度值 int xIndex = Mathf.RoundToInt(x / resolutionX); int zIndex = Mathf.RoundToInt(z / resolutionZ); heightData[zIndex, xIndex] = height; // 应用更改后的高度值到地形上 terrain.terrainData.SetHeights(0, 0, heightData); } ``` 注意:这只是一个简单的示例代码,实际应用中可能需要更多的处理和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值