12
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

HoloLens2で位置情報(WiFi)を取得する

Last updated at Posted at 2021-06-13

はじめに

HoloLens2で位置情報(WiFi)を取得してみました。

UWPの公式ドキュメントはこちら
Get the user's location

ドキュメントに書いてあるサンプルのMapControlは参考にならない
MapControl

ドキュメントのコードが書かれているのはこっち↓↓↓
Geolocation

参考までに
WindowsでGPS(みちびき含む)から現在の座標を調べてみる

PowerAppsでも取得できる
PowerAppsの信号(Signals)を試してみた

開発環境

  • Windows10 PC
  • Unity 2019.4.1f1
  • MRTK 2.7.0(Microsoft.MixedReality.Toolkit.Unity.Foundation.2.7.0.unitypackage)

導入

1.Unityで新規プロジェクト作成。MRTKの設定、MapControlオブジェクト作成。位置情報を表示するため、MainCameraの下にTextMeshProのテキストを配置。
image.png

2.MapControlオブジェクトでAddComponent、MapController.csを作成。TextMeshProのテキストをMapControlのDebugTextにアタッチ。

MapController.cs
using System.Collections;
using System.Collections.Generic;
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
#if UNITY_WSA && !UNITY_EDITOR
using Windows.Devices.Geolocation;
#endif
using TMPro;

public class MapController : MonoBehaviour
{
    public TextMeshProUGUI debugText;
    private uint _desireAccuracyInMetersValue = 0;

    // Start is called before the first frame update
    async void Start()
    {
        debugText.text = "Initialization.";

#if UNITY_WSA && !UNITY_EDITOR
        var accessStatus = await Geolocator.RequestAccessAsync();
        switch (accessStatus)
        {
            case GeolocationAccessStatus.Allowed:
                debugText.text = "Waiting for update...";
                Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = _desireAccuracyInMetersValue };
                Geoposition pos = await geolocator.GetGeopositionAsync();
                UpdateLocationData(pos);
                break;

            case GeolocationAccessStatus.Denied:
                debugText.text = "Access to location is denied.";
                break;

            case GeolocationAccessStatus.Unspecified:
                debugText.text = "Unspecified error.";
                UpdateLocationData(null);
                break;
        }
#endif
    }

#if UNITY_WSA && !UNITY_EDITOR
    private void UpdateLocationData(Geoposition position)
    {
        if (position == null)
        {
            debugText.text = "No data";
        }
        else
        {
            debugText.text = position.Coordinate.Point.Position.Latitude.ToString() + "\n" + position.Coordinate.Point.Position.Longitude.ToString();
        }
    }
#endif
}

3.Player SettingsのLocationにチェックを入れるのを忘れずに。HoloLens2のプライバシーの設定からアプリの位置情報をオンにする。

実行

お疲れ様でした。

12
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?