Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- public class DirectionFinder : MonoBehaviour
- {
- public enum TransitionType // Do not change.
- {
- WALK,
- RAIL,
- METRO_RAIL,
- SUBWAY,
- TRAM,
- MONORAIL,
- HEAVY_RAIL,
- COMMUTER_TRAIN,
- HIGH_SPEED_TRAIN,
- BUS,
- INTERCITY_BUS,
- TROLLEYBUS,
- SHARE_TAXI,
- FERRY,
- CABLE_CAR,
- GONDOLA_LIFT,
- FUNICULAR,
- OTHER
- }
- public TransitionType GetTransitionType(OnlineMapsGoogleDirectionsResult.Step step)
- {
- if (step.transit_details == null)
- return TransitionType.WALK;
- try
- {
- return (TransitionType)Enum.Parse(typeof(TransitionType), step.transit_details.line.vehicle.type);
- }
- catch (System.Exception ex)
- {
- Debug.LogException(ex);
- return TransitionType.OTHER;
- }
- }
- private Color GetColorForTransitionType(TransitionType type)
- {
- Color color = Color.magenta;
- switch (type)
- {
- case TransitionType.BUS:
- color = Color.green;
- break;
- case TransitionType.SUBWAY:
- color = Color.yellow;
- break;
- case TransitionType.TRAM:
- color = Color.white;
- break;
- }
- return color;
- }
- private void Start()
- {
- List<OnlineMapsGoogleDirectionsResult.Step> steps = new List<OnlineMapsGoogleDirectionsResult.Step>();
- FindPath("Los Angeles", "Compton", step => steps.Add(step), null,
- () =>
- {
- foreach (var step in steps)
- {
- TransitionType transitionType = GetTransitionType(step);
- OnlineMapsDrawingLine outline = new OnlineMapsDrawingLine(step.polylineD, Color.yellow, 6.0f);
- OnlineMaps.instance.AddDrawingElement(outline);
- }
- foreach (var current in steps)
- {
- var transitionType = GetTransitionType(current);
- var line = new OnlineMapsDrawingLine(new OnlineMapsVector2d[] { current.polylineD[0], current.polylineD[0] + new OnlineMapsVector2d(0, 0.000001f) }, Color.yellow, 15.0f);
- OnlineMaps.instance.AddDrawingElement(line);
- }
- foreach (var step in steps)
- {
- var transitionType = GetTransitionType(step);
- OnlineMapsDrawingLine route = new OnlineMapsDrawingLine(step.polylineD, GetColorForTransitionType(transitionType), 3.0f);
- OnlineMaps.instance.AddDrawingElement(route);
- }
- foreach (var current in steps)
- {
- var transitionType = GetTransitionType(current);
- var color = GetColorForTransitionType(transitionType);
- var darkenColor = color;
- darkenColor *= 0.5f;
- darkenColor.a = 1.0f;
- var line = new OnlineMapsDrawingLine(new OnlineMapsVector2d[] { current.polylineD[0], current.polylineD[0] + new OnlineMapsVector2d(0, 0.000001) }, color, 12.0f);
- OnlineMaps.instance.AddDrawingElement(line);
- var innerLine = new OnlineMapsDrawingLine(new OnlineMapsVector2d[] { current.polylineD[0], current.polylineD[0] + new OnlineMapsVector2d(0, 0.000001) }, darkenColor, 8.0f);
- innerLine.tooltip = current.string_instructions;
- OnlineMaps.instance.AddDrawingElement(innerLine);
- }
- });
- }
- private void FindPath(object origin, object destination, Action<OnlineMapsGoogleDirectionsResult.Step> makePathPart, Action<OnlineMapsGoogleDirectionsResult.Step, OnlineMapsGoogleDirectionsResult.Step> makeSign = null, Action onTheEnd = null)
- {
- OnlineMapsGoogleDirections.Params parameters = new OnlineMapsGoogleDirections.Params(origin, destination);
- parameters.mode = OnlineMapsGoogleDirections.Mode.transit;
- parameters.traffic_model = OnlineMapsGoogleDirections.TrafficModel.optimistic;
- OnlineMapsGoogleDirections query = OnlineMapsGoogleDirections.Find(parameters);
- query.OnComplete += resultString =>
- {
- OnlineMapsGoogleDirectionsResult result = OnlineMapsGoogleDirections.GetResult(resultString);
- // Check that the result is not null, and the number of routes is not zero.
- if (result == null || result.routes.Length == 0)
- {
- Debug.Log("Find direction failed");
- Debug.Log(resultString);
- return;
- }
- if (makePathPart != null)
- {
- foreach (var leg in result.routes[0].legs)
- foreach (var step in leg.steps)
- makePathPart(step);
- }
- if (makeSign != null)
- {
- OnlineMapsGoogleDirectionsResult.Step previous = null;
- foreach (var leg in result.routes[0].legs)
- {
- foreach (var step in leg.steps)
- {
- makeSign(previous, step);
- previous = step;
- }
- }
- }
- if (onTheEnd != null)
- onTheEnd();
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement