158 lines
4.0 KiB
C#
158 lines
4.0 KiB
C#
|
|
using ARLocation;
|
||
|
|
using System;
|
||
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public class RTKLocationProvider : AbstractLocationProvider
|
||
|
|
{
|
||
|
|
|
||
|
|
private float androidMagneticDeclination;
|
||
|
|
private AndroidNativeCompass androidNativeCompass;
|
||
|
|
public override string Name => "RTKLocationProvider";
|
||
|
|
|
||
|
|
public override bool IsCompassEnabled => Input.compass.enabled;
|
||
|
|
|
||
|
|
private long CurrentTimeMillis()
|
||
|
|
{
|
||
|
|
long currenttimemillis = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
|
||
|
|
return currenttimemillis;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override void RequestLocationAndCompassUpdates()
|
||
|
|
{
|
||
|
|
// Debug.Log("[UnityLocationProvider]: Requesting location updates...");
|
||
|
|
Input.compass.enabled = true;
|
||
|
|
|
||
|
|
//Input.location.Start(
|
||
|
|
// (float)Options.AccuracyRadius,
|
||
|
|
// (float)Options.MinDistanceBetweenUpdates
|
||
|
|
//);
|
||
|
|
//modified by thuang
|
||
|
|
if (RTKDev.GetConnectState() == 0)
|
||
|
|
RTKDev.Connect();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
protected override void InnerOnEnabled()
|
||
|
|
{
|
||
|
|
androidMagneticDeclination = AndroidMagneticDeclination.GetDeclination(CurrentLocation.ToLocation());
|
||
|
|
androidNativeCompass = new AndroidNativeCompass((float)(1.0 - LowPassFilterFactor));
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
//private static final int STATE_NONE = 0;
|
||
|
|
//private static final int STATE_CONNECTING = 1;
|
||
|
|
//private static final int STATE_CONNECTED = 2;
|
||
|
|
protected override void UpdateLocationRequestStatus()
|
||
|
|
{
|
||
|
|
switch (RTKDev.GetConnectState())
|
||
|
|
{
|
||
|
|
case 0:
|
||
|
|
Status = LocationProviderStatus.Idle;
|
||
|
|
break;
|
||
|
|
case 1:
|
||
|
|
Status = LocationProviderStatus.Initializing;
|
||
|
|
break;
|
||
|
|
case 2:
|
||
|
|
Status = LocationProviderStatus.Started;
|
||
|
|
break;
|
||
|
|
|
||
|
|
default:
|
||
|
|
Status = LocationProviderStatus.Failed;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
protected override LocationReading? ReadLocation()
|
||
|
|
{
|
||
|
|
if (!HasStarted)
|
||
|
|
{
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
double[] data = RTKDev.GetLatLongAlt();
|
||
|
|
|
||
|
|
return new LocationReading()
|
||
|
|
{
|
||
|
|
latitude = data[0],
|
||
|
|
longitude = data[1],
|
||
|
|
altitude = data[2],
|
||
|
|
accuracy = 0.05,
|
||
|
|
floor = -1,
|
||
|
|
timestamp = CurrentTimeMillis()
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
protected override HeadingReading? ReadHeading()
|
||
|
|
{
|
||
|
|
if (!HasStarted)
|
||
|
|
{
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// ReSharper disable once RedundantAssignment
|
||
|
|
var magneticHeading = Input.compass.magneticHeading;
|
||
|
|
|
||
|
|
// ReSharper disable once RedundantAssignment
|
||
|
|
var trueHeading = Input.compass.trueHeading;
|
||
|
|
|
||
|
|
#if PLATFORM_ANDROID
|
||
|
|
var tiltCorrectedMagneticHeading = GetMagneticHeading();
|
||
|
|
magneticHeading = tiltCorrectedMagneticHeading;
|
||
|
|
trueHeading = tiltCorrectedMagneticHeading + androidMagneticDeclination;
|
||
|
|
#endif
|
||
|
|
|
||
|
|
if (trueHeading < 0)
|
||
|
|
{
|
||
|
|
trueHeading += 360;
|
||
|
|
}
|
||
|
|
|
||
|
|
return new HeadingReading()
|
||
|
|
{
|
||
|
|
heading = trueHeading,
|
||
|
|
magneticHeading = magneticHeading,
|
||
|
|
accuracy = Input.compass.headingAccuracy,
|
||
|
|
timestamp = (long)(Input.compass.timestamp * 1000),
|
||
|
|
isMagneticHeadingAvailable = Input.compass.enabled
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
private float GetMagneticHeading()
|
||
|
|
{
|
||
|
|
#if PLATFORM_ANDROID
|
||
|
|
if (!SystemInfo.supportsGyroscope || !ApplyCompassTiltCompensationOnAndroid || androidNativeCompass == null)
|
||
|
|
{
|
||
|
|
return Input.compass.magneticHeading;
|
||
|
|
}
|
||
|
|
|
||
|
|
return androidNativeCompass.GetMagneticHeading();
|
||
|
|
|
||
|
|
// if (Screen.orientation == ScreenOrientation.Landscape)
|
||
|
|
// {
|
||
|
|
// return heading;// + 45;
|
||
|
|
// }
|
||
|
|
// else
|
||
|
|
// {
|
||
|
|
// return heading;
|
||
|
|
// }
|
||
|
|
|
||
|
|
#else
|
||
|
|
return Input.compass.magneticHeading;
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|