namespace Mapbox.Examples
{
using Mapbox.Unity.Location;
using Mapbox.Unity.Utilities;
using Mapbox.Unity.Map;
using UnityEngine;
public class PositionWithLocationProvider : MonoBehaviour
{
[SerializeField]
private AbstractMap _map;
///
/// The rate at which the transform's position tries catch up to the provided location.
///
[SerializeField]
float _positionFollowFactor;
///
/// Use a mock ,
/// rather than a .
///
[SerializeField]
bool _useTransformLocationProvider;
bool _isInitialized;
///
/// The location provider.
/// This is public so you change which concrete to use at runtime.
///
ILocationProvider _locationProvider;
public ILocationProvider LocationProvider
{
private get
{
if (_locationProvider == null)
{
_locationProvider = _useTransformLocationProvider ?
LocationProviderFactory.Instance.TransformLocationProvider : LocationProviderFactory.Instance.DefaultLocationProvider;
}
return _locationProvider;
}
set
{
if (_locationProvider != null)
{
_locationProvider.OnLocationUpdated -= LocationProvider_OnLocationUpdated;
}
_locationProvider = value;
_locationProvider.OnLocationUpdated += LocationProvider_OnLocationUpdated;
}
}
Vector3 _targetPosition;
void Start()
{
LocationProvider.OnLocationUpdated += LocationProvider_OnLocationUpdated;
_map.OnInitialized += () => _isInitialized = true;
}
void OnDestroy()
{
if (LocationProvider != null)
{
LocationProvider.OnLocationUpdated -= LocationProvider_OnLocationUpdated;
}
}
void LocationProvider_OnLocationUpdated(Location location)
{
if (_isInitialized && location.IsLocationUpdated)
{
_targetPosition = _map.GeoToWorldPosition(location.LatitudeLongitude);
}
}
void Update()
{
transform.localPosition = Vector3.Lerp(transform.localPosition, _targetPosition, Time.deltaTime * _positionFollowFactor);
}
}
}