forked from chadly/Geocoding.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBingMapsGeocoder.cs
More file actions
270 lines (231 loc) · 7.88 KB
/
BingMapsGeocoder.cs
File metadata and controls
270 lines (231 loc) · 7.88 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Geocoding.Microsoft
{
/// <remarks>
/// http://msdn.microsoft.com/en-us/library/ff701715.aspx
/// </remarks>
public class BingMapsGeocoder : IGeocoder
{
const string UNFORMATTED_QUERY = "http://dev.virtualearth.net/REST/v1/Locations/{0}?key={1}";
const string FORMATTED_QUERY = "http://dev.virtualearth.net/REST/v1/Locations?{0}&key={1}";
const string QUERY = "q={0}";
const string COUNTRY = "countryRegion={0}";
const string ADMIN = "adminDistrict={0}";
const string ZIP = "postalCode={0}";
const string CITY = "locality={0}";
const string ADDRESS = "addressLine={0}";
readonly string bingKey;
public IWebProxy Proxy { get; set; }
public string Culture { get; set; }
public Location UserLocation { get; set; }
public Bounds UserMapView { get; set; }
public IPAddress UserIP { get; set; }
public BingMapsGeocoder(string bingKey)
{
if (string.IsNullOrWhiteSpace(bingKey))
throw new ArgumentException("bingKey can not be null or empty");
this.bingKey = bingKey;
}
private string GetQueryUrl(string address)
{
var parameters = new StringBuilder();
bool first = true;
first = AppendParameter(parameters, address, QUERY, first);
first = AppendGlobalParameters(parameters, first);
return string.Format(FORMATTED_QUERY, parameters.ToString(), bingKey);
}
private string GetQueryUrl(string street, string city, string state, string postalCode, string country)
{
StringBuilder parameters = new StringBuilder();
bool first = true;
first = AppendParameter(parameters, city, CITY, first);
first = AppendParameter(parameters, state, ADMIN, first);
first = AppendParameter(parameters, postalCode, ZIP, first);
first = AppendParameter(parameters, country, COUNTRY, first);
first = AppendParameter(parameters, street, ADDRESS, first);
first = AppendGlobalParameters(parameters, first);
return string.Format(FORMATTED_QUERY, parameters.ToString(), bingKey);
}
private string GetQueryUrl(double latitude, double longitude)
{
var builder = new StringBuilder(string.Format(UNFORMATTED_QUERY, string.Format(CultureInfo.InvariantCulture, "{0},{1}", latitude, longitude), bingKey));
AppendGlobalParameters(builder, false);
return builder.ToString();
}
private IEnumerable<KeyValuePair<string, string>> GetGlobalParameters()
{
if (!string.IsNullOrEmpty(Culture))
yield return new KeyValuePair<string, string>("c", Culture);
if (UserLocation != null)
yield return new KeyValuePair<string, string>("userLocation", UserLocation.ToString());
if (UserMapView != null)
yield return new KeyValuePair<string, string>("userMapView", string.Concat(UserMapView.SouthWest.ToString(), ",", UserMapView.NorthEast.ToString()));
if (UserIP != null)
yield return new KeyValuePair<string, string>("userIp", UserIP.ToString());
}
private bool AppendGlobalParameters(StringBuilder parameters, bool first)
{
var values = GetGlobalParameters().ToArray();
if (!first) parameters.Append("&");
parameters.Append(BuildQueryString(values));
return first && !values.Any();
}
private string BuildQueryString(IEnumerable<KeyValuePair<string, string>> parameters)
{
var builder = new StringBuilder();
foreach (var pair in parameters)
{
if (builder.Length > 0) builder.Append("&");
builder.Append(BingUrlEncode(pair.Key));
builder.Append("=");
builder.Append(BingUrlEncode(pair.Value));
}
return builder.ToString();
}
public async Task<IEnumerable<BingAddress>> GeocodeAsync(string address)
{
try
{
var url = GetQueryUrl(address);
var response = await GetResponse(url).ConfigureAwait(false);
return ParseResponse(response);
}
catch (Exception ex)
{
throw new BingGeocodingException(ex);
}
}
public async Task<IEnumerable<BingAddress>> GeocodeAsync(string street, string city, string state, string postalCode, string country)
{
try
{
var url = GetQueryUrl(street, city, state, postalCode, country);
var response = await GetResponse(url).ConfigureAwait(false);
return ParseResponse(response);
}
catch (Exception ex)
{
throw new BingGeocodingException(ex);
}
}
public async Task<IEnumerable<BingAddress>> ReverseGeocodeAsync(Location location)
{
if (location == null)
throw new ArgumentNullException("location");
return await ReverseGeocodeAsync(location.Latitude, location.Longitude).ConfigureAwait(false);
}
public async Task<IEnumerable<BingAddress>> ReverseGeocodeAsync(double latitude, double longitude)
{
try
{
var url = GetQueryUrl(latitude, longitude);
var response = await GetResponse(url).ConfigureAwait(false);
return ParseResponse(response);
}
catch (Exception ex)
{
throw new BingGeocodingException(ex);
}
}
async Task<IEnumerable<Address>> IGeocoder.GeocodeAsync(string address)
{
return await GeocodeAsync(address).ConfigureAwait(false);
}
async Task<IEnumerable<Address>> IGeocoder.GeocodeAsync(string street, string city, string state, string postalCode, string country)
{
return await GeocodeAsync(street, city, state, postalCode, country).ConfigureAwait(false);
}
async Task<IEnumerable<Address>> IGeocoder.ReverseGeocodeAsync(Location location)
{
return await ReverseGeocodeAsync(location).ConfigureAwait(false);
}
async Task<IEnumerable<Address>> IGeocoder.ReverseGeocodeAsync(double latitude, double longitude)
{
return await ReverseGeocodeAsync(latitude, longitude).ConfigureAwait(false);
}
private bool AppendParameter(StringBuilder sb, string parameter, string format, bool first)
{
if (!string.IsNullOrEmpty(parameter))
{
if (!first)
{
sb.Append('&');
}
sb.Append(string.Format(format, BingUrlEncode(parameter)));
return false;
}
return first;
}
private IEnumerable<BingAddress> ParseResponse(Json.Response response)
{
var list = new List<BingAddress>();
foreach (Json.Location location in response.ResourceSets[0].Resources)
{
list.Add(new BingAddress(
location.Address.FormattedAddress,
new Location(location.Point.Coordinates[0], location.Point.Coordinates[1]),
location.Address.AddressLine,
location.Address.AdminDistrict,
location.Address.AdminDistrict2,
location.Address.CountryRegion,
location.Address.Locality,
location.Address.PostalCode,
(EntityType)Enum.Parse(typeof(EntityType), location.EntityType),
EvaluateConfidence(location.Confidence)
));
}
return list;
}
private HttpRequestMessage CreateRequest(string url)
{
return new HttpRequestMessage(HttpMethod.Get, url);
}
HttpClient BuildClient()
{
if (this.Proxy == null)
return new HttpClient();
var handler = new HttpClientHandler();
handler.Proxy = this.Proxy;
return new HttpClient(handler);
}
private async Task<Json.Response> GetResponse(string queryURL)
{
using (var client = BuildClient())
{
var response = await client.SendAsync(CreateRequest(queryURL)).ConfigureAwait(false);
using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Json.Response));
return jsonSerializer.ReadObject(stream) as Json.Response;
}
}
}
private ConfidenceLevel EvaluateConfidence(string confidence)
{
switch (confidence.ToLower())
{
case "low":
return ConfidenceLevel.Low;
case "medium":
return ConfidenceLevel.Medium;
case "high":
return ConfidenceLevel.High;
default:
return ConfidenceLevel.Unknown;
}
}
private string BingUrlEncode(string toEncode)
{
if (string.IsNullOrEmpty(toEncode))
return string.Empty;
return WebUtility.UrlEncode(toEncode);
}
}
}