- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleGeocodingClient.cs
More file actions
Latest commit
74 lines (65 loc) · 2.32 KB
/
Copy pathGoogleGeocodingClient.cs
File metadata and controls
74 lines (65 loc) · 2.32 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
usingSystem;
usingSystem.Collections.Generic;
usingNewtonsoft.Json;
usingSystem.Threading.Tasks;
usingSystem.Globalization;
usingGoogleGeocodingClient.Transport;
usingGoogleGeocodingClient.Api;
namespaceGoogleGeocodingClient
{
/// <summary>
/// Wrapper for returning results from Google's Geocoding API.
/// </summary>
publicstaticclassGoogleGeocodingClient
{
/// <summary>
/// Obtain the geographic coordinates of the specified address. May return multiple sets
/// of coordinates.
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
publicasyncstaticTask<List<GeographicCoordinate>>Geocode(stringaddress)
{
returnawaitGeocode(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>
publicasyncstaticTask<List<GeographicCoordinate>>Geocode(stringaddress,boolsensor)
{
varrequestParams=newDictionary<string,string>
{
{"address",address},
//{"key", ApiKey},
{"sensor",sensor.ToString().ToLowerInvariant()},
{"output","json"},
{"oe","utf8"}
};
varresponse=awaitGeocodingRequest.Execute(requestParams);
returnReadResult(response);
}
#region Helpers
privatestaticList<GeographicCoordinate>ReadResult(GeocodingResponseresponse)
{
List<GeographicCoordinate>coords=newList<GeographicCoordinate>();
if(response.Results==null||response.Results.Length==0)
{
returncoords;
}
foreach(GeocodingResultresultinresponse.Results)
{
coords.Add(newGeographicCoordinate
{
Latitude=double.Parse(result.Geometry.Location.Latitude,CultureInfo.InvariantCulture),
Longitude=double.Parse(result.Geometry.Location.Longitude,CultureInfo.InvariantCulture)
});
}
returncoords;
}
#endregion
}
}