138 lines
4.3 KiB
C#
138 lines
4.3 KiB
C#
using System;
|
||
using System.Threading.Tasks;
|
||
using GoogleApi.Entities.Common;
|
||
using GoogleApi.Entities.Common.Enums;
|
||
using GoogleApi.Entities.Maps.Common;
|
||
using GoogleApi.Entities.Maps.Common.Enums;
|
||
using GoogleApi.Entities.Maps.DistanceMatrix.Request;
|
||
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using System.Reflection.Metadata;
|
||
using TIAMWebApp.Shared.Application.Models;
|
||
using static DevExpress.XtraPrinting.Native.ExportOptionsPropertiesNames;
|
||
using System.Net;
|
||
using GoogleApi.Entities.Maps.DistanceMatrix.Response;
|
||
|
||
namespace TIAMWebApp.Server.Controllers
|
||
{
|
||
[ApiController]
|
||
[Route("[controller]")]
|
||
public class GoogleAPIController : ControllerBase
|
||
{
|
||
|
||
|
||
|
||
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;
|
||
}
|
||
|
||
[HttpGet]
|
||
[Route("GetAddressForCoordinates")]
|
||
public string GetAddressForCoordinates(TripInfo myTrip)
|
||
{
|
||
|
||
var latitude = Trips[0].StartLatitude; // Example latitude
|
||
|
||
var longitude = Trips[0].StartLongitude; // Example longitude
|
||
Console.WriteLine(latitude);
|
||
Console.WriteLine(longitude);
|
||
|
||
try
|
||
{
|
||
/*var address = locationService.GetAddressFromLatLang(latitude, longitude);
|
||
Console.WriteLine(address.Address);
|
||
string myaddress = address.Address;
|
||
return "myaddress: " + myaddress;*/
|
||
return "";
|
||
}
|
||
catch (System.Net.WebException ex)
|
||
{
|
||
Console.WriteLine("Google Maps API Error {0}", ex.Message);
|
||
return "Google Maps API Error {0}" + ex.Message;
|
||
}
|
||
}
|
||
|
||
[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
|
||
|
||
Console.WriteLine(latitude1);
|
||
Console.WriteLine(longitude1);
|
||
Console.WriteLine(latitude2);
|
||
Console.WriteLine(longitude2);
|
||
|
||
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 = "AIzaSyAyEYJkpt2KDa3SJ34UNWO4-dNOJKmUtF8",
|
||
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}", (int)response.Status);
|
||
|
||
}
|
||
|
||
|
||
return "";
|
||
|
||
}
|
||
catch (WebException ex)
|
||
{
|
||
Console.WriteLine("Google Maps API Error {0}", ex.Message);
|
||
return "Google Maps API Error {0}" + ex.Message;
|
||
}
|
||
|
||
|
||
}
|
||
|
||
}
|
||
} |