using System;
using UnityEngine;
///
/// Emulates route movement
///
public class GpsRouteEmulator : MonoBehaviour
{
///
/// Start location name
///
public string origin = "Los Angeles";
///
/// End location name
///
public string destination = "Hollywood";
///
/// Speed of movement (km/h).
///
[Range(0, 150)]
public float speed = 60;
///
/// Minimum distance (km) between points to update GPS position
///
public float updateDistance = 0.005f;
///
/// Reference to Location Service
///
private OnlineMapsLocationService locationService;
///
/// Array of route points
///
private OnlineMapsVector2d[] points;
///
/// Current point index
///
private int pointIndex = -1;
///
/// Current step progress
///
private double progress;
///
/// Current point on the route
///
private OnlineMapsVector2d currentPosition;
///
/// Last emulated point on the route in Location Service
///
private OnlineMapsVector2d emulatedPosition;
private void OnDirectionsComplete(string response)
{
OnlineMapsGoogleDirectionsResult result = OnlineMapsGoogleDirections.GetResult(response);
if (result == null || result.routes.Length == 0)
{
Debug.Log("Google Direction API failed!!!");
Debug.Log(response);
return;
}
OnlineMapsGoogleDirectionsResult.Route firstRoute = result.routes[0];
// Gets points of route.
points = firstRoute.overview_polylineD;
// Draw the route.
OnlineMapsDrawingLine route = new OnlineMapsDrawingLine(points, Color.red, 3);
OnlineMapsDrawingElementManager.AddItem(route);
pointIndex = 0;
}
private void Start()
{
locationService = OnlineMapsLocationService.instance;
if (locationService == null)
{
Debug.LogError("Cannot find Location Service");
Destroy(this);
return;
}
if (!OnlineMapsKeyManager.hasGoogleMaps)
{
Debug.LogError("Key Manager / Google Maps is empty");
Destroy(this);
return;
}
OnlineMapsGoogleDirections.Find(new OnlineMapsGoogleDirections.Params(origin, destination)).OnComplete += OnDirectionsComplete;
}
private void Update()
{
if (pointIndex == -1) return;
// Start point
OnlineMapsVector2d p1 = points[pointIndex];
// End point
OnlineMapsVector2d p2 = points[pointIndex + 1];
// Total step distance
double dx, dy;
OnlineMapsUtils.DistanceBetweenPoints(p1.x, p1.y, p2.x, p2.y, out dx, out dy);
double stepDistance = Math.Sqrt(dx * dx + dy * dy);
// Total step time
double totalTime = stepDistance / speed * 3600;
// Current step progress
progress += Time.deltaTime / totalTime;
// Update current position and compass value
if (progress < 1)
{
currentPosition = OnlineMapsVector2d.Lerp(p1, p2, progress);
locationService.emulatorCompass = 450 - OnlineMapsUtils.Angle2D(p1, p2);
}
else
{
currentPosition = p2;
pointIndex++;
progress = 0;
if (pointIndex >= points.Length - 1)
{
Debug.Log("Finish");
pointIndex = -1;
}
else
{
locationService.emulatorCompass = 450 - OnlineMapsUtils.Angle2D(p2, points[pointIndex + 1]) / 360;
}
}
// Distance between current position and last emulated position
OnlineMapsUtils.DistanceBetweenPoints(currentPosition.x, currentPosition.y, emulatedPosition.x, emulatedPosition.y, out dx, out dy);
// If the distance is greater that updateDistance, update emulator position
if (dx * dx + dy * dy > updateDistance * updateDistance)
{
locationService.emulatorPosition = emulatedPosition = currentPosition;
}
}
}