//----------------------------------------------------------------------- // // Copyright (c) 2016 Mapbox. All rights reserved. // //----------------------------------------------------------------------- namespace Mapbox.Geocoding { using System; using System.Text; using Mapbox.Json; using Mapbox.Platform; using Mapbox.Utils.JsonConverters; /// /// Wrapper around the /// Mapbox Geocoding API. The Geocoder does two things: geocoding and reverse geocoding. /// public sealed class Geocoder { private readonly IFileSource fileSource; /// Initializes a new instance of the class. /// Network access abstraction. public Geocoder(IFileSource fileSource) { this.fileSource = fileSource; } /// 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 Geocode(GeocodeResource geocode, Action callback) { return this.fileSource.Request( geocode.GetUrl(), (Response response) => { var str = Encoding.UTF8.GetString(response.Data); var data = Deserialize(str); callback(data); }); } /// 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 Geocode(GeocodeResource geocode, Action callback) { return this.fileSource.Request( geocode.GetUrl(), (Response response) => { var str = Encoding.UTF8.GetString(response.Data); var data = Deserialize(str); callback(data); }); } /// /// Deserialize the geocode response string into a . /// /// JSON String. /// A . /// Forward or reverse geocode. public T Deserialize(string str) { return JsonConvert.DeserializeObject(str, JsonConverters.Converters); } } }