-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathProgram.cs
118 lines (95 loc) · 3.69 KB
/
Program.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Maps;
using Google.Maps.Geocoding;
using Google.Maps.Common;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
README_QuickStart_Sample1();
DoRequestsLoop();
if(System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine("Hit any key to end.");
Console.ReadKey();
}
}
static void README_QuickStart_Sample1()
{
//always need to use YOUR_API_KEY for requests. Do this in App_Start.
GoogleSigned.AssignAllServices(new GoogleSigned("YOUR_API_KEY"));
var request = new GeocodingRequest();
request.Address = "1600 Pennsylvania Ave NW, Washington, DC 20500";
var response = new GeocodingService().GetResponse(request);
//The GeocodingService class submits the request to the API web service, and returns the
//response strongly typed as a GeocodeResponse object which may contain zero, one or more results.
//Assuming we received at least one result, let's get some of its properties:
if(response.Status == ServiceResponseStatus.Ok && response.Results.Count() > 0)
{
var result = response.Results.First();
Console.WriteLine("Full Address: " + result.FormattedAddress); // "1600 Pennsylvania Ave NW, Washington, DC 20500, USA"
Console.WriteLine("Latitude: " + result.Geometry.Location.Latitude); // 38.8976633
Console.WriteLine("Longitude: " + result.Geometry.Location.Longitude); // -77.0365739
Console.WriteLine();
}
else
{
Console.WriteLine("Unable to geocode. Status={0} and ErrorMessage={1}", response.Status, response.ErrorMessage);
}
}
static void DoRequestsLoop()
{
Dictionary<string, Action> menuchoice = new Dictionary<string, Action>(StringComparer.OrdinalIgnoreCase);
menuchoice.Add("G", DoGeocodeRequest);
//menuchoice.Add("Q", null);
ConsoleKeyInfo keypress;
do
{
Console.WriteLine("Menu\n--------");
Console.WriteLine("G)eocode request");
Console.WriteLine("Q)uit");
keypress = Console.ReadKey(); Console.WriteLine();
var key = keypress.KeyChar.ToString();
Action actionfunc = null;
if(menuchoice.ContainsKey(key) ==true)
{
actionfunc = menuchoice[key];
if(actionfunc != null) actionfunc();
}
} while(keypress.Key != ConsoleKey.Q);
}
static void DoGeocodeRequest()
{
//always need to use YOUR_API_KEY for requests. Do this in App_Start.
//GoogleSigned.AssignAllServices(new GoogleSigned("YOUR_API_KEY"));
//commented out in the loop
Console.WriteLine();
Console.WriteLine("Enter an address to geocode: ");
string geocodeAddress = Console.ReadLine();
var request = new GeocodingRequest();
request.Address = geocodeAddress;
var response = new GeocodingService().GetResponse(request);
//The GeocodingService class submits the request to the API web service, and returns the
//response strongly typed as a GeocodeResponse object which may contain zero, one or more results.
//Assuming we received at least one result, let's get some of its properties:
if(response.Status == ServiceResponseStatus.Ok && response.Results.Count() > 0)
{
var result = response.Results.First();
Console.WriteLine("Full Address: " + result.FormattedAddress); // "1600 Pennsylvania Ave NW, Washington, DC 20500, USA"
Console.WriteLine("Latitude: " + result.Geometry.Location.Latitude); // 38.8976633
Console.WriteLine("Longitude: " + result.Geometry.Location.Longitude); // -77.0365739
Console.WriteLine();
}
else
{
Console.WriteLine("Unable to geocode. Status={0} and ErrorMessage={1}", response.Status, response.ErrorMessage);
}
}
}
}