-
-
Save garethpaul/5b3505ecc72314931b7c1d1df8f5f712 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Mapbox.Examples.LocationProvider | |
{ | |
using Mapbox.Unity.Location; | |
using Mapbox.Unity.Utilities; | |
using Mapbox.Unity.Map; | |
using UnityEngine; | |
public class PositionWithLocationProvider : MonoBehaviour | |
{ | |
[SerializeField] | |
private AbstractMap _map; | |
public FoursquareProvider foursquareProvider; | |
private bool fsqStarted = false; | |
/// <summary> | |
/// The rate at which the transform's position tries catch up to the provided location. | |
/// </summary> | |
[SerializeField] | |
float _positionFollowFactor; | |
/// <summary> | |
/// Use a mock <see cref="T:Mapbox.Unity.Location.TransformLocationProvider"/>, | |
/// rather than a <see cref="T:Mapbox.Unity.Location.EditorLocationProvider"/>. | |
/// </summary> | |
[SerializeField] | |
bool _useTransformLocationProvider; | |
bool _isInitialized; | |
/// <summary> | |
/// The location provider. | |
/// This is public so you change which concrete <see cref="T:Mapbox.Unity.Location.ILocationProvider"/> to use at runtime. | |
/// </summary> | |
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(object sender, LocationUpdatedEventArgs e) | |
{ | |
//Debug.Log ("Hello Location Provider Update"); | |
if (_isInitialized) | |
{ | |
_targetPosition = Conversions.GeoToWorldPosition(e.Location, | |
_map.CenterMercator, | |
_map.WorldRelativeScale).ToVector3xz(); | |
if (!fsqStarted) { | |
fsqStarted = true; | |
foursquareProvider.PlaceFoursquare (e.Location); | |
} | |
} | |
} | |
void Update() | |
{ | |
transform.position = Vector3.Lerp(transform.position, _targetPosition, Time.deltaTime * _positionFollowFactor); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment