using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Newtonsoft.Json; using System.Data; namespace Mip { public class AddressComponent { public string long_name { get; set; } public string short_name { get; set; } public List types { get; set; } } public class Northeast { public double lat { get; set; } public double lng { get; set; } } public class Southwest { public double lat { get; set; } public double lng { get; set; } } public class Bounds { public Northeast northeast { get; set; } public Southwest southwest { get; set; } } public class Location { public double lat { get; set; } public double lng { get; set; } } public class Northeast2 { public double lat { get; set; } public double lng { get; set; } } public class Southwest2 { public double lat { get; set; } public double lng { get; set; } } public class Viewport { public Northeast2 northeast { get; set; } public Southwest2 southwest { get; set; } } public class Geometry { public Bounds bounds { get; set; } public Location location { get; set; } public string location_type { get; set; } public Viewport viewport { get; set; } } public class Result { public List address_components { get; set; } public string formatted_address { get; set; } public Geometry geometry { get; set; } public List types { get; set; } } public class RootObject { public List results { get; set; } public string status { get; set; } } class classMapa { public static double zemSirka; public static double zemDlzka; public static DataTable dataTable; public static void SearchGoogleAPI(string hladanyNazov, string paramArr ) { RootObject rootObj = new RootObject(); //http://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034&sensor=false //http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false&key=API_KEY string hs = "http://maps.googleapis.com/maps/api/geocode/json?" + paramArr + hladanyNazov + "&sensor=false"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(hs); try { WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); string str = reader.ReadToEnd(); rootObj = null; rootObj = JsonConvert.DeserializeObject(str); if (rootObj.status == "OK") { DataTable dataTab2 = new DataTable(); dataTab2.Columns.Add("Hľadaný názov", typeof(string)); dataTab2.Columns.Add("Číslo", typeof(string)); dataTab2.Columns.Add("Ulica", typeof(string)); dataTab2.Columns.Add("Názov", typeof(string)); dataTab2.Columns.Add("Č.D.", typeof(string)); dataTab2.Columns.Add("PSČ", typeof(string)); dataTab2.Columns.Add("Krajina", typeof(string)); dataTab2.Columns.Add("Šírka", typeof(double)); dataTab2.Columns.Add("Dĺžka", typeof(double)); dataTab2.Columns.Add("ViewSV-šírka", typeof(double)); dataTab2.Columns.Add("ViewSV-dížka", typeof(double)); dataTab2.Columns.Add("ViewJZ-šírka", typeof(double)); dataTab2.Columns.Add("ViewJZ-dížka", typeof(double)); foreach (Result rs in rootObj.results) { DataRow newDataRow = dataTab2.NewRow(); zemSirka = rs.geometry.location.lat; zemDlzka = rs.geometry.location.lng; foreach (AddressComponent ac in rs.address_components) { if (ac.types[0] == "street_number") newDataRow["Číslo"] = ac.long_name.ToString(); if (ac.types[0] == "route") newDataRow["Ulica"] = ac.long_name.ToString(); if (ac.types[0] == "locality") newDataRow["Názov"] = ac.long_name.ToString(); if (ac.types[0] == "country") newDataRow["Krajina"] = ac.long_name.ToString(); if (ac.types[0] == "postal_code") newDataRow["PSČ"] = ac.long_name.ToString(); if (ac.types[0] == "premise") newDataRow["Č.D."] = ac.long_name.ToString(); } newDataRow["Hľadaný názov"] = rs.formatted_address; newDataRow["Šírka"] = rs.geometry.location.lat; newDataRow["Dĺžka"] = rs.geometry.location.lng; newDataRow["ViewSV-šírka"] = rs.geometry.viewport.northeast.lat; newDataRow["ViewSV-dížka"] = rs.geometry.viewport.northeast.lng; newDataRow["ViewJZ-šírka"] = rs.geometry.viewport.southwest.lat; newDataRow["ViewJZ-dížka"] = rs.geometry.viewport.southwest.lng; dataTab2.Rows.Add(newDataRow); dataTab2.AcceptChanges(); } dataTable = dataTab2; } } } catch (WebException ex) { WebResponse errorResponse = ex.Response; using (Stream responseStream = errorResponse.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); String errorText = reader.ReadToEnd(); // log errorText } throw; } } } }