using System; using System.Windows.Forms; using System.Diagnostics; using Newtonsoft.Json.Linq; using System.Net.Http; using System.Threading.Tasks; namespace GPSNavigator { public partial class MainForm : Form { private TextBox txtStartLat; private TextBox txtStartLng; private TextBox txtEndLat; private TextBox txtEndLng; private Button btnNavigate; private Label lblResult; public MainForm() { InitializeComponent(); InitializeForm(); } private void InitializeForm() { txtStartLat = CreateTextBox("Начальная широта", 10); txtStartLng = CreateTextBox("Начальная долгота", 40); txtEndLat = CreateTextBox("Конечная широта", 70); txtEndLng = CreateTextBox("Конечная долгота", 100); btnNavigate = new Button { Location = new System.Drawing.Point(10, 130), Text = "Навигация" }; btnNavigate.Click += btnNavigate_Click; lblResult = new Label { Location = new System.Drawing.Point(10, 160), AutoSize = true }; // Добавление элементов на форму this.Controls.Add(txtStartLat); this.Controls.Add(txtStartLng); this.Controls.Add(txtEndLat); this.Controls.Add(txtEndLng); this.Controls.Add(btnNavigate); this.Controls.Add(lblResult); } private TextBox CreateTextBox(string placeholder, int yPosition) { return new TextBox { Location = new System.Drawing.Point(10, yPosition), PlaceholderText = placeholder }; } private async void btnNavigate_Click(object sender, EventArgs e) { try { double startLat = double.Parse(txtStartLat.Text); double startLng = double.Parse(txtStartLng.Text); double endLat = double.Parse(txtEndLat.Text); double endLng = double.Parse(txtEndLng.Text); string url = $"https://maps.googleapis.com/maps/api/directions/json?origin={startLat},{startLng}&destination={endLat},{endLng}"; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { string jsonResponse = await response.Content.ReadAsStringAsync(); JObject json = JObject.Parse(jsonResponse); string distance = json["routes"][0]["legs"][0]["distance"]["text"].ToString(); string duration = json["routes"][0]["legs"][0]["duration"]["text"].ToString(); lblResult.Text = $"Расстояние: {distance}, Время в пути: {duration}"; } else { lblResult.Text = "Ошибка при получении данных"; } } } catch (FormatException) { lblResult.Text = "Некорректный формат координат"; } catch (Exception ex) { lblResult.Text = $"Ошибка: {ex.Message}"; } } } }