-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathGeocoder.cs
More file actions
93 lines (82 loc) · 3.26 KB
/
Geocoder.cs
File metadata and controls
93 lines (82 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//-----------------------------------------------------------------------
// <copyright file="Geocoder.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using Mapbox.BaseModule.Data.Platform;
using Mapbox.BaseModule.Utilities.JsonConverters;
using Newtonsoft.Json;
namespace Mapbox.GeocodingApi
{
using System;
using System.Text;
/// <summary>
/// Wrapper around the <see href="https://www.mapbox.com/api-documentation/search/#geocoding">
/// Mapbox Geocoding API</see>. The Geocoder does two things: geocoding and reverse geocoding.
/// </summary>
public sealed class MapboxGeocodingApi
{
private readonly IFileSource fileSource;
/// <summary> Initializes a new instance of the <see cref="MapboxGeocodingApi" /> class. </summary>
/// <param name="fileSource"> Network access abstraction. </param>
public MapboxGeocodingApi(IFileSource fileSource)
{
this.fileSource = fileSource;
}
/// <summary> Performs asynchronously a geocoding lookup. </summary>
/// <param name="geocode"> Geocode resource. </param>
/// <param name="callback"> Callback to be called after the request is completed. </param>
/// <typeparam name="T"> String or LngLat. Should be automatically inferred. </typeparam>
/// <returns>
/// Returns a <see cref="IAsyncRequest" /> 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.
/// </returns>
public IAsyncRequest Geocode<T>(GeocodeResource<T> geocode, Action<ReverseGeocodeResponse> callback)
{
return this.fileSource.Request(
geocode.GetUrl(),
(Response response) =>
{
if (response.Data == null)
{
callback(null);
return;
}
var str = Encoding.UTF8.GetString(response.Data);
var data = Deserialize<ReverseGeocodeResponse>(str);
callback(data);
});
}
/// <summary> Performs asynchronously a geocoding lookup. </summary>
/// <param name="geocode"> Geocode resource. </param>
/// <param name="callback"> Callback to be called after the request is completed. </param>
/// <typeparam name="T"> String or LngLat. Should be automatically inferred. </typeparam>
/// <returns>
/// Returns a <see cref="IAsyncRequest" /> 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.
/// </returns>
public IAsyncRequest Geocode<T>(GeocodeResource<T> geocode, Action<ForwardGeocodeResponse> callback)
{
return this.fileSource.Request(
geocode.GetUrl(),
(Response response) =>
{
var str = Encoding.UTF8.GetString(response.Data);
var data = Deserialize<ForwardGeocodeResponse>(str);
callback(data);
});
}
/// <summary>
/// Deserialize the geocode response string into a <see cref="GeocodeResponse"/>.
/// </summary>
/// <param name="str">JSON String.</param>
/// <returns>A <see cref="GeocodeResponse"/>.</returns>
/// <typeparam name="T">Forward or reverse geocode. </typeparam>
public T Deserialize<T>(string str)
{
return JsonConvert.DeserializeObject<T>(str, JsonConverters.Converters);
}
}
}