Unity之GPS定位(腾讯sdk)
Unity之GPS定位(腾讯sdk)
目录
前言
这段时间在写项目的时候,需要用到GPS逆地址解析,小黑果断就拿出来了之前写的一篇文章Unity之GPS定位(高德),但是经过商量,决定用腾讯地图的SDK腾讯位置服务,于是我们就开始了,也有了这篇文章。
Unity版本及使用插件
Unity版本: Unity2020.3.13f1c1
命名空间: Newtonsoft.Json
那么,废话不多说,之前每篇博客废话太多,自己都开始嫌弃了。。
正题
编写脚本
第一个脚本:别忘了挂载
public class TencentLocation : MonoBehaviour
{
/// <summary>
/// 获取当前位置及周围信息
/// </summary>
/// <param name="lat">纬度</param>
/// <param name="lng">经度</param>
/// <param name="callback">当前位置信息</param>
/// <returns></returns>
public IEnumerator GetRequest(float lat, float lng, Action<LocationAnalysis> callback)
{
Uri uri = new Uri("https://apis.map.qq.com/ws/geocoder/v1/?location=" + $"{lat},{lng}" + "&key= 你在腾讯官网申请的Key &get_poi=1");
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
if (webRequest.result == UnityWebRequest.Result.Success)
{
System.Object obj = JsonConvert.DeserializeObject(webRequest.downloadHandler.text);
Newtonsoft.Json.Linq.JObject js = obj as Newtonsoft.Json.Linq.JObject;
if (js == null)
yield return null;
callback?.Invoke(js["result"].ToObject<LocationAnalysis>());
}
}
}
}
第二个脚本:这个脚本可花了我不少时间,小黑要赞!
using System.Collections.Generic;
namespace TencentInverseAnalysis
{
/// <summary>
/// 地址解析
/// </summary>
public class LocationAnalysis
{
/// <summary>
/// 经纬度位置
/// </summary>
public Location location { get; set; }
/// <summary>
/// 北京市朝阳区广顺北大街
/// </summary>
public string address { get; set; }
/// <summary>
/// 格式化地址
/// </summary>
public Formatted_addresses formatted_addresses { get; set; }
/// <summary>
/// 地址成分
/// </summary>
public Address_component address_component { get; set; }
/// <summary>
/// 地址信息
/// </summary>
public Ad_info ad_info { get; set; }
/// <summary>
/// 地址参考
/// </summary>
public Address_reference address_reference { get; set; }
/// <summary>
/// 信息点数量
/// </summary>
public int poi_count { get; set; }
/// <summary>
/// 信息点
/// </summary>
public List<PoisItem> pois { get; set; }
}
/// <summary>
/// 经纬度位置
/// </summary>
public class Location
{
/// <summary>
/// 纬度
/// </summary>
public double lat { get; set; }
/// <summary>
/// 经度
/// </summary>
public double lng { get; set; }
}
/// <summary>
/// 格式化后的地址
/// </summary>
public class Formatted_addresses
{
/// <summary>
/// 推荐定位地址
/// </summary>
public string recommend { get; set; }
/// <summary>
/// 粗糙定位地址
/// </summary>
public string rough { get; set; }
}
/// <summary>
/// 地址成分
/// </summary>
public class Address_component
{
/// <summary>
/// 国家
/// </summary>
public string nation { get; set; }
/// <summary>
/// 省份
/// </summary>
public string province { get; set; }
/// <summary>
/// 市
/// </summary>
public string city { get; set; }
/// <summary>
/// 区
/// </summary>
public string district { get; set; }
/// <summary>
/// 街道
/// </summary>
public string street { get; set; }
/// <summary>
/// 街道号
/// </summary>
public string street_number { get; set; }
}
/// <summary>
/// 地址信息
/// </summary>
public class Ad_info
{
/// <summary>
/// 国家编码
/// </summary>
public string nation_code { get; set; }
/// <summary>
/// 省份编码
/// </summary>
public string adcode { get; set; }
/// <summary>
/// 城市编码
/// </summary>
public string city_code { get; set; }
/// <summary>
/// 位置名
/// </summary>
public string name { get; set; }
/// <summary>
/// 经纬度位置
/// </summary>
public Location location { get; set; }
/// <summary>
/// 国家
/// </summary>
public string nation { get; set; }
/// <summary>
/// 省份
/// </summary>
public string province { get; set; }
/// <summary>
/// 市
/// </summary>
public string city { get; set; }
/// <summary>
/// 区
/// </summary>
public string district { get; set; }
}
/// <summary>
/// 地址参考
/// </summary>
public class Address_reference
{
/// <summary>
///
/// </summary>
public Street_number street_number { get; set; }
/// <summary>
///
/// </summary>
public Business_area business_area { get; set; }
/// <summary>
///
/// </summary>
public Famous_area famous_area { get; set; }
/// <summary>
///
/// </summary>
public Crossroad crossroad { get; set; }
/// <summary>
///
/// </summary>
public Town town { get; set; }
/// <summary>
///
/// </summary>
public Street street { get; set; }
/// <summary>
///
/// </summary>
public Landmark_l2 landmark_l2 { get; set; }
}
#region 地址参考
/// <summary>
/// 街道号码
/// </summary>
public class Street_number
{
/// <summary>
/// 街道ID
/// </summary>
public string id { get; set; }
/// <summary>
/// 街道台头
/// </summary>
public string title { get; set; }
/// <summary>
/// 街道经纬度位置
/// </summary>
public Location location { get; set; }
/// <summary>
/// 街道据当前距离
/// </summary>
public double _distance { get; set; }
/// <summary>
/// 街道据当前方向
/// </summary>
public string _dir_desc { get; set; }
}
/// <summary>
/// 所在经营范围
/// </summary>
public class Business_area
{
/// <summary>
/// 范围ID
/// </summary>
public string id { get; set; }
/// <summary>
/// 范围台头
/// </summary>
public string title { get; set; }
/// <summary>
/// 范围经纬度
/// </summary>
public Location location { get; set; }
/// <summary>
/// 距离
/// </summary>
public int _distance { get; set; }
/// <summary>
/// 方向
/// </summary>
public string _dir_desc { get; set; }
}
/// <summary>
/// 范围内已注册信息点
/// </summary>
public class Famous_area
{
/// <summary>
/// 店铺ID
/// </summary>
public string id { get; set; }
/// <summary>
/// 店铺台头
/// </summary>
public string title { get; set; }
/// <summary>
/// 店铺位置
/// </summary>
public Location location { get; set; }
/// <summary>
/// 店铺距离
/// </summary>
public int _distance { get; set; }
/// <summary>
/// 店铺方向
/// </summary>
public string _dir_desc { get; set; }
}
/// <summary>
/// 十字路口
/// </summary>
public class Crossroad
{
/// <summary>
/// 十字路口ID
/// </summary>
public string id { get; set; }
/// <summary>
/// 路口台头
/// </summary>
public string title { get; set; }
/// <summary>
/// 路口经纬度位置
/// </summary>
public Location location { get; set; }
/// <summary>
/// 路口据当前位置距离
/// </summary>
public double _distance { get; set; }
/// <summary>
/// 路口据当前位置方向
/// </summary>
public string _dir_desc { get; set; }
}
/// <summary>
/// 区块
/// </summary>
public class Town
{
/// <summary>
/// 区块ID
/// </summary>
public string id { get; set; }
/// <summary>
/// 区块台头
/// </summary>
public string title { get; set; }
/// <summary>
/// 区块位置
/// </summary>
public Location location { get; set; }
/// <summary>
/// 区块据当前位置距离
/// </summary>
public int _distance { get; set; }
/// <summary>
/// 区块据当前位置方向
/// </summary>
public string _dir_desc { get; set; }
}
/// <summary>
/// 街道
/// </summary>
public class Street
{
/// <summary>
/// 街道ID
/// </summary>
public string id { get; set; }
/// <summary>
/// 街道台头
/// </summary>
public string title { get; set; }
/// <summary>
/// 街道经纬度位置信息
/// </summary>
public Location location { get; set; }
/// <summary>
/// 街道据当前位置距离
/// </summary>
public double _distance { get; set; }
/// <summary>
/// 街道据当前位置方向
/// </summary>
public string _dir_desc { get; set; }
}
/// <summary>
/// 地标信息
/// </summary>
public class Landmark_l2
{
/// <summary>
/// 地标ID
/// </summary>
public string id { get; set; }
/// <summary>
/// 地标台头
/// </summary>
public string title { get; set; }
/// <summary>
/// 地标经纬度位置
/// </summary>
public Location location { get; set; }
/// <summary>
/// 地标据当前位置距离
/// </summary>
public int _distance { get; set; }
/// <summary>
/// 地标据当前位置方向
/// </summary>
public string _dir_desc { get; set; }
}
#endregion
/// <summary>
/// 信息点
/// </summary>
public class PoisItem
{
/// <summary>
/// 信息点ID
/// </summary>
public string id { get; set; }
/// <summary>
/// 信息点台头
/// </summary>
public string title { get; set; }
/// <summary>
/// 信息点所在地
/// </summary>
public string address { get; set; }
/// <summary>
/// 信息点种类
/// </summary>
public string category { get; set; }
/// <summary>
/// 信息点经纬度位置
/// </summary>
public Location location { get; set; }
/// <summary>
/// 信息点位置信息
/// </summary>
public Ad_info ad_info { get; set; }
/// <summary>
/// 距离当前经纬度的相对距离
/// </summary>
public int _distance { get; set; }
/// <summary>
/// 据当前位置方向
/// </summary>
public string _dir_desc { get; set; }
}
}
Run运行,
跑起来就行,具体要什么,去相关类找就好了。
没有腾讯地图SDK的Key?
来,让小黑带你去找。
首先打开人家的官网腾讯位置服务 。
然后右上角登录 或者 注册。
完成后,还是点击右上方的控制台。
然后创建一个应用
创建完成后,增加一个key
接着你就有Key值了。然后拿着你的这个key添加到代码中,就ok了。
至此,圆满结束。
希望大家:点赞,留言,关注咯~
唠家常
- 小黑的今日分享结束啦,小伙伴们你们get到了么,你们有没有更好的办法呢,可以评论区留言分享,也可以加小黑的QQ:841298494,大家一起进步。
今日无推荐
- 客官,看完get之后记得点赞哟!
- 小伙伴你还想要别的知识?好的呀,分享给你们
- 小黑的杂货铺,想要什么都有,客官不进来喝杯茶么?
Unity之GPS定位(腾讯sdk)的更多相关文章
- Unity之GPS定位(高德解析)
Unity之GPS定位 Unity之GPS定位(高德解析) 前言 开篇 Unity版本及使用插件 正题 创建场景 写脚本 把脚本挂载到场景中 打包发布场景 安装真机并且测试 代码中的==Key==怎么 ...
- Android开发之位置定位详解与实例解析(GPS定位、Google网络定位,BaiduLBS(SDK)定位)
在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便.定位一般分为三种发方案:即GPS定位.Google网络定位以及基站定位 最简单的手机定位方式当然是通过GP ...
- ArcGIS Runtime SDK for Android 定位权限(GPS定位\网络定位)
ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION: android.permission.ACCESS_COARSE_LOCATION:是基站定位,即基于无线网络 ...
- GPS定位解决偏差
目录 GPS定位解决偏差 开篇 实践 1.解决思路以及步骤 2.实践出真理! 3.上坐标系之间的代码. 希望大家:点赞,留言,关注咯~ 唠家常 今日推荐都在文章中了 GPS定位解决偏差 开篇 大家都知 ...
- GPS定位学习笔记
********************************* GPS定位简介 ********************************** 1. iOS SDK提供两个框架来实现位置服务 ...
- 与众不同 windows phone (20) - Device(设备)之位置服务(GPS 定位), FM 收音机, 麦克风, 震动器
原文:与众不同 windows phone (20) - Device(设备)之位置服务(GPS 定位), FM 收音机, 麦克风, 震动器 [索引页][源码下载] 与众不同 windows phon ...
- [置顶]
xamarin android使用gps定位获取经纬度
看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...
- GPS定位 测试
public class MainActivity extends Activity { private final String TAG = "BX"; private Loca ...
- Android中GPS定位的简单应用
在Android中通过GPS获得当前位置,首先要获得一个LocationManager实例,通过该实例的getLastKnownLocation()方法获得第一个的位置,该方法的说明如下: void ...
随机推荐
- 【笔记】CF1607F Robot on the Board 2 及相关
题目传送门 记忆化搜索 首先,这题 \(10000\) 组 \(2000\times 2000\) 的数据直接爆搜肯定会超时.想到,如果一个点的答案已经被更新过,之后走到这个点能再多走的点也就确定了, ...
- 嵌入式-C语言基础:指针数组(和数组指针区分开来)
指针数组:一个数组,若其元素均为指针类型的数据,称为指针数组,指针数组存放的是指针类型的数据,也就是指针数组的每个元素都存放一个地址.下面定义一个指针数组: int * p[4];//[]的优先级是比 ...
- 使用Jupyter记事本记录和制作.NET可视化笔记
前言:对于记录笔记的工具特别多,不过对于程序员来说,记录笔记+程序代码+运行结果演示可以同时存在,无疑会极大增加我们的笔记的可读性和体验感.以前在写python的时候,使用jupyter的体验很好,所 ...
- polkit(ploicykit)特权提升漏洞解决方案
一.[概述] polkit 的 pkexec 存在本地权限提升漏洞,已获得普通权限的攻击者可通过此漏洞获取root权限,漏洞利用难度低. pkexec是一个Linux下Polkit里的setuid工具 ...
- github访问慢怎么办
前言 访问github网速老不好?老掉线?下载贼慢?或许这篇笔记可以帮助你! Github访问慢的根本原因其实是CDN内容分发受到DNS污染,无法连接使用igithub的加速分发服务器,所以国内访问时 ...
- 【Java并发入门】02 Java内存模型:看Java如何解决可见性和有序性问题
如何解决其中的可见性和有序性导致的问题,这也就引出来了今天的主角--Java 内存模型. 一.什么是 Java 内存模型? 导致可见性的原因是缓存,导致有序性的原因是编译优化,那解决可见性.有序性最直 ...
- Zabbix与乐维监控对比分析(一)——架构、性能篇
近年来,Zabbix凭借其近乎无所不能的监控及优越的性能一路高歌猛进,在开源监控领域独占鳌头:而作为后起的新锐IT监控平台--乐维监控,则不断吸收Zabbix,Prometheus等优秀开源平台的优点 ...
- PostgreSQL常用操作合辑:时间日期、系统函数、正则表达式、库表导入导出、元数据查询、自定义函数、常用案例
〇.参考地址 1.pg官方文档 http://www.postgres.cn/docs/9.6/index.html 2.腾讯云仓pg文档 https://cloud.tencent.com/docu ...
- 【每日一题】【奇偶分别中心扩展/动态规划】2022年2月5日-NC最长回文子串的长度
描述对于长度为n的一个字符串A(仅包含数字,大小写英文字母),请设计一个高效算法,计算其中最长回文子串的长度. 方法1:奇数偶数分别从中心扩展 import java.util.*; public c ...
- Boolean.getBoolean() 与 Boolean.parseBoolean()
1. 问题回顾 当在不了解 Boolean 中的 getBoolean() 方法与 parseBoolean() 方法的区别时,在使用过程中就会出现不明所以的bug. 比如如下使用情况: // isA ...