Program.cs 3.73 KB
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; }
    }

}