-
Notifications
You must be signed in to change notification settings - Fork 2
/
GoogleGeocodingClient.cs
74 lines (65 loc) · 2.32 KB
/
GoogleGeocodingClient.cs
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
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Threading.Tasks;
using System.Globalization;
using GoogleGeocodingClient.Transport;
using GoogleGeocodingClient.Api;
namespace GoogleGeocodingClient
{
/// <summary>
/// Wrapper for returning results from Google's Geocoding API.
/// </summary>
public static class GoogleGeocodingClient
{
/// <summary>
/// Obtain the geographic coordinates of the specified address. May return multiple sets
/// of coordinates.
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public async static Task<List<GeographicCoordinate>> Geocode(string address)
{
return await Geocode(address, false);
}
/// <summary>
/// Obtain the geographic coordinates of the specified address. May return multiple sets
/// of coordinates.
/// </summary>
/// <param name="address"></param>
/// <param name="sensor">Use of the Google Maps API now requires that you indicate whether your application is using a sensor (such as a GPS locator) to determine the user's location. Applications that determine the user's location via a sensor must true.</param>
/// <returns></returns>
public async static Task<List<GeographicCoordinate>> Geocode(string address, bool sensor)
{
var requestParams = new Dictionary<string, string>
{
{"address", address},
//{"key", ApiKey},
{"sensor", sensor.ToString().ToLowerInvariant()},
{"output", "json"},
{"oe", "utf8"}
};
var response = await GeocodingRequest.Execute(requestParams);
return ReadResult(response);
}
#region Helpers
private static List<GeographicCoordinate> ReadResult(GeocodingResponse response)
{
List<GeographicCoordinate> coords = new List<GeographicCoordinate>();
if (response.Results == null || response.Results.Length == 0)
{
return coords;
}
foreach (GeocodingResult result in response.Results)
{
coords.Add(new GeographicCoordinate
{
Latitude = double.Parse(result.Geometry.Location.Latitude, CultureInfo.InvariantCulture),
Longitude = double.Parse(result.Geometry.Location.Longitude, CultureInfo.InvariantCulture)
});
}
return coords;
}
#endregion
}
}