//----------------------------------------------------------------------- // // Copyright (c) 2017 Mapbox. All rights reserved. // //----------------------------------------------------------------------- namespace Mapbox.MapMatching { using System; using System.Text; using Mapbox.Json; using Mapbox.Platform; using Mapbox.Utils.JsonConverters; /// /// Wrapper around the /// Mapbox Map Matching API. /// public class MapMatcher { private readonly IFileSource _fileSource; private int _timeout; /// Initializes a new instance of the class. /// Network access abstraction. public MapMatcher(IFileSource fileSource, int timeout) { _fileSource = fileSource; _timeout = timeout; } /// Performs asynchronously a geocoding lookup. /// Geocode resource. /// Callback to be called after the request is completed. /// String or LngLat. Should be automatically inferred. /// /// Returns a that can be used for canceling a pending /// request. This handle can be completely ignored if there is no intention of ever /// canceling the request. /// public IAsyncRequest Match(MapMatchingResource match, Action callback) { string url = match.GetUrl(); return _fileSource.Request( url, (Response response) => { var str = Encoding.UTF8.GetString(response.Data); var data = Deserialize(str); if (response.HasError) { data.SetRequestExceptions(response.Exceptions); } callback(data); }, _timeout ); } /// /// Deserialize the map match response string into a . /// /// JSON String. /// A . /// Map Matcher. internal T Deserialize(string str) { return JsonConvert.DeserializeObject(str, JsonConverters.Converters); } } }