本日はMapSDK枠です。
〇MapSDKとは?
[Microsoft Map SDK for Untiy](MapSDK)はMicrosoftyによって公開されているMixedRealityデバイス向けのUnityPackageです。
MapSDKを使用することでMixedReality空間上に3Dの地図を表示することができます。
今回はこのMapSDKを使用してユーザーの周辺地図を表示していきます。
〇ユーザの自己位置情報取得
HoloLensではスマートフォンなどと異なりGPSが搭載されていないため、正確な位置情報を得ることはできません。
しかしWifiを使用することで大雑把なユーザーの位置情報を得ることができます。
これはデスクトップPCなどのデバイスで位置情報に基づいた広告の表示や天気予報が表示される仕組みと同じであります。
今回熊本のxRコミュニティKumaMCNの質問箱でわが師ガチ本さんに「HoloLensで位置情報ってどうやって取得するのですかね?」と質問したところその日のうちにQiitaでまとめて教えてくださいました。
今回このコードを応用してMapSDKの位置に反映させます。
〇コード
今回上述Qiitaの記事のコードに追加を行いました。
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;
using Microsoft.Maps.Unity; //MapRendererを使用するために追加
using Microsoft.Geospatial; //LatLon型を使用するために追加
public class GetPlayerLocation : MonoBehaviour
{
//MapSDKでMapの描画を行うMapRendererを指定する。
public MapRenderer mapRenderer;
public TextMeshPro 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();
//追加 MapRendererの座標に取得した位置情報を代入、ズームレベルを17設定し400m四方が表示されるように設定。
mapRenderer.SetMapScene(new MapSceneOfLocationAndZoomLevel(new LatLon(position.Coordinate.Point.Position.Latitude, position.Coordinate.Point.Position.Longitude), 17f));
}
}
#endif
}
このスクリプトをゲームオブジェクトに設定し、MapSDKのMapRendererを指定します。
MapSDKのプロダクト設定に関しては過去の記事を参考にしてください。
ビルドする前に[PlayerSettings]の[Capability]から[Location]にチェックを入れて位置情報へのアクセスを許可します。

デプロイしネット環境下で実行することで現在地周辺のマップが表示されることを確認できます。