无需接入SDK即可在Unity中获取经纬度(Android/iOS),告诉我你的坐标
发布日期:2021-06-30 19:35:20 浏览次数:2 分类:技术文章

本文共 1818 字,大约阅读时间需要 6 分钟。

Unity获取经纬度

Unity提供了Input.location,方便我们获取经纬度。不过,只能获取经纬度,如果想要通过经纬度得出具体省份城市,则需要通过SDK接口了。

测试结果

场景如下

在这里插入图片描述
测试结果如下
在这里插入图片描述

代码

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class Main : MonoBehaviour {
public Text infoText; public Button getLocationBtn; void Start() {
getLocationBtn.onClick.AddListener (() => {
StartCoroutine (StartLocate ()); }); } IEnumerator StartLocate() {
if (!Input.location.isEnabledByUser) {
Input.location.Start(); infoText.text = "false == location.isEnabledByUser"; yield break; } // Start service before querying location Input.location.Start(); // Wait until service initializes int maxWait = 20; while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {
yield return new WaitForSeconds(1); maxWait--; } // Service didn't initialize in 20 seconds if (maxWait < 1) {
var logStr = "Timed out"; infoText.text = logStr; Debug.Log (logStr); yield break; } // Connection has failed if (Input.location.status == LocationServiceStatus.Failed) {
var logStr = "Unable to determine device location"; infoText.text = logStr; Debug.Log (logStr); yield break; } else {
// Access granted and location value could be retrieved var logStr = "Location, 纬度: " + Input.location.lastData.latitude + ",经度: " + Input.location.lastData.longitude + ",高度: " + Input.location.lastData.altitude + "\n水平精确度: " + Input.location.lastData.horizontalAccuracy + ",时间戳: " + Input.location.lastData.timestamp; infoText.text = logStr; Debug.Log (logStr); //取出位置的经纬度 string str = Input.location.lastData.longitude + "," + Input.location.lastData.latitude; } // Stop service if there is no need to query location updates continuously Input.location.Stop(); }}

转载地址:https://linxinfa.blog.csdn.net/article/details/107523164 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Unity获取系统信息SystemInfo(CPU、显卡、操作系统等信息)
下一篇:Unity打开照相机与打开本地相册然后在Unity中显示照片(Android与iOS)

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月06日 16时49分58秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章