197 lines
5.9 KiB
C#
197 lines
5.9 KiB
C#
using AyCode.Core.Server.Loggers;
|
||
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<SupplierAPIController> _logger;
|
||
|
||
|
||
public GoogleAPIController(ILogger<SupplierAPIController> logger)
|
||
{
|
||
_logger = logger;
|
||
|
||
}
|
||
|
||
[HttpPost]
|
||
[Route("GetAddressForCoordinates")]
|
||
public async Task<string?> GetAddressForCoordinates(TripInfo myTrip)
|
||
{
|
||
|
||
var latitude = myTrip.StartLatitude; // Example latitude
|
||
|
||
var longitude = myTrip.StartLongitude; // Example longitude
|
||
GlobalLogger.Info(latitude.ToString());
|
||
GlobalLogger.Info(longitude.ToString());
|
||
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<string> ValidateAddress(string address)
|
||
{
|
||
|
||
var request = new AddressValidationRequest
|
||
{
|
||
Key = _apiKey,
|
||
Address = new PostalAddress
|
||
{
|
||
AddressLines = new List<string>
|
||
{
|
||
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<string> 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
|
||
|
||
GlobalLogger.Info(latitude1.ToString());
|
||
GlobalLogger.Info(longitude1.ToString());
|
||
GlobalLogger.Info(latitude2.ToString());
|
||
GlobalLogger.Info(longitude2.ToString());
|
||
|
||
try
|
||
{
|
||
|
||
var origin1 = new Address("Margit utca 35, Budapest, Budapest, Magyarorsz<73>g");
|
||
var origin2 = new Address("Nefelejcs utca 18, Budapest, Budapest, Magyarorsz<73>g");
|
||
var destination1 = new Address("Sz<53>zados utca 30/a, Budapest, Budapest, Magyarorsz<73>g");
|
||
var destination2 = new Address("Novoszadek utca 53, Pilissz<73>nt<6E>, Pest, Magyarorsz<73>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)
|
||
{
|
||
GlobalLogger.Error($"Errorcode: {response?.Status}, {e.Message}", e);
|
||
}
|
||
|
||
|
||
return "";
|
||
|
||
}
|
||
catch (WebException ex)
|
||
{
|
||
GlobalLogger.Error($"Google Maps API Error {ex.Message}", ex);
|
||
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; }
|
||
}
|
||
|
||
}
|
||
} |