//----------------------------------------------------------------------- // // Copyright (c) 2016 Mapbox. All rights reserved. // //----------------------------------------------------------------------- namespace Mapbox.Examples.Playground { using Mapbox.Unity; using System; using UnityEngine; using UnityEngine.UI; using Mapbox.Json; using Mapbox.Directions; using Mapbox.Utils; using Mapbox.Utils.JsonConverters; using Mapbox.Geocoding; /// /// Fetch directions JSON once start and end locations are provided. /// Example: Enter Start Location: San Francisco, Enter Destination: Los Angeles /// public class DirectionsExample : MonoBehaviour { [SerializeField] Text _resultsText; [SerializeField] ForwardGeocodeUserInput _startLocationGeocoder; [SerializeField] ForwardGeocodeUserInput _endLocationGeocoder; Directions _directions; Vector2d[] _coordinates; DirectionResource _directionResource; void Start() { _directions = MapboxAccess.Instance.Directions; _startLocationGeocoder.OnGeocoderResponse += StartLocationGeocoder_OnGeocoderResponse; _endLocationGeocoder.OnGeocoderResponse += EndLocationGeocoder_OnGeocoderResponse; _coordinates = new Vector2d[2]; // Can we make routing profiles an enum? _directionResource = new DirectionResource(_coordinates, RoutingProfile.Driving); _directionResource.Steps = true; } void OnDestroy() { if (_startLocationGeocoder != null) { _startLocationGeocoder.OnGeocoderResponse -= StartLocationGeocoder_OnGeocoderResponse; } if (_startLocationGeocoder != null) { _startLocationGeocoder.OnGeocoderResponse -= EndLocationGeocoder_OnGeocoderResponse; } } /// /// Start location geocoder responded, update start coordinates. /// /// Sender. /// E. void StartLocationGeocoder_OnGeocoderResponse(ForwardGeocodeResponse response) { _coordinates[0] = _startLocationGeocoder.Coordinate; if (ShouldRoute()) { Route(); } } /// /// End location geocoder responded, update end coordinates. /// /// Sender. /// E. void EndLocationGeocoder_OnGeocoderResponse(ForwardGeocodeResponse response) { _coordinates[1] = _endLocationGeocoder.Coordinate; if (ShouldRoute()) { Route(); } } /// /// Ensure both forward geocoders have a response, which grants access to their respective coordinates. /// /// true, if both forward geocoders have a response, false otherwise. bool ShouldRoute() { return _startLocationGeocoder.HasResponse && _endLocationGeocoder.HasResponse; } /// /// Route /// void Route() { _directionResource.Coordinates = _coordinates; _directions.Query(_directionResource, HandleDirectionsResponse); } /// /// Log directions response to UI. /// /// Res. void HandleDirectionsResponse(DirectionsResponse res) { var data = JsonConvert.SerializeObject(res, Formatting.Indented, JsonConverters.Converters); string sub = data.Substring(0, data.Length > 5000 ? 5000 : data.Length) + "\n. . . "; _resultsText.text = sub; } } }