Program.cs
3.73 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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Ingeneo.Console.Geo
{
class Program
{
static void Main(string[] args)
{
// Create a request for the URL.
string apiKey = ConfigurationManager.AppSettings.Get("Google.API.Key");
#region Leer Excel
List<string> direcciones = new List<string>() { "carrera+48a+10+sur+25" };
#endregion
foreach (string item in direcciones)
{
var location = GetGeo(item, apiKey);
#region Escribir Excel
//location
//formatted_address
#endregion
}
}
public static Result GetGeo(string address, string apiKey)
{
Result geoResponse = new Result();
try
{
string url = string.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}", address, apiKey);
WebRequest request = WebRequest.Create(url);
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
//System.Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
// The using block ensures the stream is automatically closed.
using (Stream dataStream = response.GetResponseStream())
{
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
geoResponse = JsonConvert.DeserializeObject<Result>(responseFromServer);
// Display the content.
//System.Console.WriteLine(responseFromServer);
}
// Close the response.
response.Close();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
return geoResponse;
}
}
public class Result
{
public List<GEOResponse> results { get; set; }
public string status { get; set; }
}
public class GEOResponse {
public List<AddressComponents> address_components { get; set; }
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
}
public class AddressComponents
{
public string long_name { get; set; }
public string short_name { get; set; }
public string[] types { get; set; }
}
public class Geometry {
public Location location { get; set; }
public string location_type { get; set; }
public ViewPort view_port { get; set; }
public string place_id { get; set; }
public PlusCode plus_code { get; set; }
public string[] types { get; set; }
}
public class Location
{
public float lat { get; set; }
public float lng { get; set; }
}
public class ViewPort {
public Location northeast { get; set; }
public Location southwest { get; set; }
}
public class PlusCode
{
public string compound_code { get; set; }
public string global_code { get; set; }
}
}