-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathProgram.cs
More file actions
55 lines (45 loc) · 1.49 KB
/
Copy pathProgram.cs
File metadata and controls
55 lines (45 loc) · 1.49 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
using GoogleMapsApi;
using GoogleMapsApi.Entities.Geocoding.Request;
using GoogleMapsApi.Entities.Geocoding.Response;
string? address = args.Length > 0
? string.Join(' ', args)
: PromptForAddress();
if (string.IsNullOrWhiteSpace(address))
{
Console.Error.WriteLine("No address provided. Pass it as args or type one when prompted.");
return 1;
}
string? apiKey = Environment.GetEnvironmentVariable("GOOGLE_API_KEY");
if (string.IsNullOrWhiteSpace(apiKey))
{
Console.Error.WriteLine("GOOGLE_API_KEY environment variable is not set.");
return 2;
}
var request = new GeocodingRequest
{
Address = address,
ApiKey = apiKey,
};
// Instance-based client (preferred). In a real app, resolve IGoogleMapsClient from
// dependency injection via AddHttpClient<IGoogleMapsClient, GoogleMapsClient>().
using var httpClient = new HttpClient();
IGoogleMapsClient maps = new GoogleMapsClient(httpClient);
GeocodingResponse response = await maps.Geocode.QueryAsync(request);
if (response.Status != Status.OK || response.Results is null)
{
Console.Error.WriteLine($"Geocoding failed: {response.Status}");
return 3;
}
foreach (var result in response.Results)
{
var location = result.Geometry.Location;
Console.WriteLine($"Address : {result.FormattedAddress}");
Console.WriteLine($"Lat/Lng : {location.Latitude:F6}, {location.Longitude:F6}");
Console.WriteLine();
}
return 0;
static string? PromptForAddress()
{
Console.Write("Address: ");
return Console.ReadLine();
}