using GoogleApi; using GoogleApi.Entities.Common; using GoogleApi.Entities.Common.Enums; using GoogleApi.Entities.Maps.AddressValidation.Request; using GoogleApi.Entities.Maps.Common; using GoogleApi.Entities.Maps.Common.Enums; using GoogleApi.Entities.Maps.Directions.Response; using GoogleApi.Entities.Maps.DistanceMatrix.Request; using GoogleApi.Entities.Maps.DistanceMatrix.Response; using GoogleApi.Entities.Maps.Geocoding.Location.Request; using Microsoft.AspNetCore.Mvc; using System; using System.Net; using TIAMWebApp.Shared.Application.Models; namespace TIAMWebApp.Server.Controllers { [ApiController] [Route("api/v1/[controller]")] public class GoogleAPIController : ControllerBase { private string _apiKey = "AIzaSyAyEYJkpt2KDa3SJ34UNWO4-dNOJKmUtF8"; private static readonly TripInfo[] Trips = new TripInfo[] { new TripInfo(47.511887f, 19.031920f, 47.510769f, 19.081422f ), }; private readonly ILogger _logger; public GoogleAPIController(ILogger logger) { _logger = logger; } [HttpPost] [Route("GetAddressForCoordinates")] public async Task GetAddressForCoordinates(TripInfo myTrip) { var latitude = myTrip.StartLatitude; // Example latitude var longitude = myTrip.StartLongitude; // Example longitude Console.WriteLine(latitude); Console.WriteLine(longitude); var booo = new Coordinate(latitude, longitude); var request = new LocationGeocodeRequest { Key = _apiKey, Location = booo }; try { var response = await GoogleMaps.Geocode.LocationGeocode.QueryAsync(request); if (response.Status == Status.Ok) { return response.RawJson; } else { return "Something wrong"; } } catch (Exception ex) { return ex + "Error occurred while calling GoogleMaps.Geocode.LocationGeocode.QueryAsync"; } } //google api call to get coordinates from address [HttpPost] [Route("ValidateAddress")] public async Task ValidateAddress(string address) { var request = new AddressValidationRequest { Key = _apiKey, Address = new PostalAddress { AddressLines = new List { address } } }; var response = await GoogleMaps.AddressValidation.QueryAsync(request); if (response.Status == Status.Ok) { return response.Result.Address.FormattedAddress; } else if (response.Status == Status.ZeroResults) { return "No results found for the given address."; } else { return "Something went wrong."; } } [HttpGet] [Route("GetTravelTime")] //public string GetTravelTime(TripInfo) public async Task GetTravelTimeWithGoogleMatrix() { var latitude1 = Trips[0].StartLatitude; // Example latitude var longitude1 = Trips[0].StartLongitude; // Example longitude var latitude2 = Trips[0].EndLatitude; // Example latitude var longitude2 = Trips[0].EndLongitude; // Example longitude Console.WriteLine(latitude1); Console.WriteLine(longitude1); Console.WriteLine(latitude2); Console.WriteLine(longitude2); try { var origin1 = new Address("Margit utca 35, Budapest, Budapest, Magyarország"); var origin2 = new Address("Nefelejcs utca 18, Budapest, Budapest, Magyarország"); var destination1 = new Address("Százados utca 30/a, Budapest, Budapest, Magyarország"); var destination2 = new Address("Novoszadek utca 53, Pilisszántó, Pest, Magyarország"); DistanceMatrixResponse? response = null; //var response = await GoogleMaps.DistanceMatrix.QueryAsync(request); try { var request = new DistanceMatrixRequest { Key = _apiKey, Origins = new[] { new LocationEx(origin1), new LocationEx(origin2) }, Destinations = new[] { new LocationEx(destination1), new LocationEx(destination2) }, TravelMode = TravelMode.DRIVING, }; response = await GoogleApi.GoogleMaps.DistanceMatrix.QueryAsync(request); return response.RawJson; } catch (Exception e) { Console.Write("Errorcode: {0}, {1}", (int)response.Status, e); } return ""; } catch (WebException ex) { Console.WriteLine("Google Maps API Error {0}", ex.Message); return "Google Maps API Error {0}" + ex.Message; } } public class GoogleMapsResponse { public string Status { get; set; } public Result[] Results { get; set; } } public class Result { public string FormattedAddress { get; set; } } } }