Skip to content
Snippets Groups Projects
Commit 627c3130 authored by LE-CUDENEC JOFFREY's avatar LE-CUDENEC JOFFREY
Browse files

Récupération des données D'un Trajet entre deux destinations

parent 340e7969
Branches
Tags
1 merge request!15Récupération des données D'un Trajet entre deux destinations
using Mapbox.Unity;
using UnityEngine;
using UnityEngine.UI;
using System;
using Mapbox.Geocoding;
using Mapbox.Utils;
[RequireComponent(typeof(InputField))]
public class ForwardGeocodeUserInput : MonoBehaviour
{
InputField _inputField;
ForwardGeocodeResource _resource;
Vector2d _coordinate;
public Vector2d Coordinate
{
get
{
return _coordinate;
}
}
bool _hasResponse;
public bool HasResponse
{
get
{
return _hasResponse;
}
}
public ForwardGeocodeResponse Response { get; private set; }
public event Action<ForwardGeocodeResponse> OnGeocoderResponse = delegate { };
void Awake()
{
_inputField = GetComponent<InputField>();
_inputField.onEndEdit.AddListener(HandleUserInput);
_resource = new ForwardGeocodeResource("");
}
void HandleUserInput(string searchString)
{
_hasResponse = false;
if (!string.IsNullOrEmpty(searchString))
{
_resource.Query = searchString;
MapboxAccess.Instance.Geocoder.Geocode(_resource, HandleGeocoderResponse);
}
}
void HandleGeocoderResponse(ForwardGeocodeResponse res)
{
_hasResponse = true;
if (null == res)
{
_inputField.text = "no geocode response";
}
else if (null != res.Features && res.Features.Count > 0)
{
var center = res.Features[0].Center;
_coordinate = res.Features[0].Center;
}
Response = res;
OnGeocoderResponse(res);
}
}
fileFormatVersion: 2
guid: 624cf9dc8f2652e459b56928068e7af3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

using System.Collections;
using Mapbox.Unity.Location;
using Mapbox.Unity.Map;
using UnityEngine;
public class InitializeMapWithLocationProvider : MonoBehaviour
{
[SerializeField]
AbstractMap _map;
ILocationProvider _locationProvider;
private void Awake()
{
// Prevent double initialization of the map.
_map.InitializeOnStart = false;
}
protected virtual IEnumerator Start()
{
yield return null;
_locationProvider = LocationProviderFactory.Instance.DefaultLocationProvider;
_locationProvider.OnLocationUpdated += _locationProvider_OnLocationUpdated;
}
private void _locationProvider_OnLocationUpdated(Location location)
{
_locationProvider.OnLocationUpdated -= _locationProvider_OnLocationUpdated;
_map.Initialize(location.LatitudeLongitude, _map.AbsoluteZoom);
}
}
fileFormatVersion: 2
guid: 62c2d5ef077adbe42bdb049218092d8b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
This diff is collapsed.
fileFormatVersion: 2
guid: 9697e11316b0dca48b49a28f1c41eb5b
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
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;
public class destination : MonoBehaviour
{
[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;
}
private void _endLocationGeocoder_OnGeocoderResponse(ForwardGeocodeResponse obj)
{
_coordinates[1] = _endLocationGeocoder.Coordinate;
if (ShouldRoute())
{
Route();
}
}
private void _startLocationGeocoder_OnGeocoderResponse(ForwardGeocodeResponse obj)
{
_coordinates[0] = _startLocationGeocoder.Coordinate;
if (ShouldRoute())
{
Route();
}
}
/// <summary>
/// Ensure both forward geocoders have a response, which grants access to their respective coordinates.
/// </summary>
/// <returns><c>true</c>, if both forward geocoders have a response, <c>false</c> otherwise.</returns>
bool ShouldRoute()
{
return _startLocationGeocoder.HasResponse && _endLocationGeocoder.HasResponse;
}
/// <summary>
/// Route
/// </summary>
void Route()
{
_directionResource.Coordinates = _coordinates;
_directions.Query(_directionResource, HandleDirectionsResponse);
}
/// <summary>
/// Log directions response to UI.
/// </summary>
/// <param name="res">Res.</param>
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. . . ";
System.IO.File.WriteAllText(Application.dataPath + "\\test.json", data);
}
}
fileFormatVersion: 2
guid: a00f8a4d6e35ff7439cc585130aad999
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
This diff is collapsed.
fileFormatVersion: 2
guid: 25243e07c0233b44b82d4e684e696cfd
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment