104 lines
3.2 KiB
C#
104 lines
3.2 KiB
C#
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace TIAM.Services.Server
|
|
{
|
|
public class GooglePlacesService
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private readonly string _apiKey = "AIzaSyAyEYJkpt2KDa3SJ34UNWO4-dNOJKmUtF8";
|
|
|
|
public GooglePlacesService(HttpClient httpClient)
|
|
{
|
|
_httpClient = httpClient;
|
|
}
|
|
|
|
public async Task<string> ValidateAddressAsync(string address)
|
|
{
|
|
var requestUri = $"https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input={address}&inputtype=textquery&fields=formatted_address,name&key={_apiKey}";
|
|
|
|
var response = await _httpClient.GetFromJsonAsync<GooglePlacesResponse>(requestUri);
|
|
|
|
return response?.Candidates?.Length > 0 ? "Valid address" : "Invalid address";
|
|
}
|
|
|
|
public async Task<string> CalculateRouteAsync(string origin, string destination, DateTime travelTime)
|
|
{
|
|
// Convert the travel time to Unix time
|
|
long unixTime = ((DateTimeOffset)travelTime).ToUnixTimeSeconds();
|
|
var requestUri = $"https://maps.googleapis.com/maps/api/directions/json?origin={origin}&destination={destination}&departure_time={unixTime}&key={_apiKey}";
|
|
|
|
var response = await _httpClient.GetFromJsonAsync<GoogleDirectionsResponse>(requestUri);
|
|
|
|
if (response?.Routes?.Length > 0)
|
|
{
|
|
var route = response.Routes[0];
|
|
return $"Distance: {route.Legs[0].Distance.Text}, Duration: {route.Legs[0].Duration.Text} (with traffic: {route.Legs[0].DurationInTraffic?.Text})";
|
|
}
|
|
|
|
return "Route not found";
|
|
}
|
|
|
|
public async Task<List<string>> GetPlacePredictionsAsync(string input)
|
|
{
|
|
var requestUri = $"https://maps.googleapis.com/maps/api/place/autocomplete/json?input={input}&key={_apiKey}";
|
|
|
|
var response = await _httpClient.GetFromJsonAsync<GooglePlacesAutocompleteResponse>(requestUri);
|
|
|
|
var predictions = new List<string>();
|
|
if (response?.Predictions != null)
|
|
{
|
|
foreach (var prediction in response.Predictions)
|
|
{
|
|
predictions.Add(prediction.Description);
|
|
}
|
|
}
|
|
|
|
return predictions;
|
|
}
|
|
}
|
|
|
|
public class GooglePlacesAutocompleteResponse
|
|
{
|
|
public Prediction[] Predictions { get; set; }
|
|
}
|
|
|
|
public class Prediction
|
|
{
|
|
public string Description { get; set; }
|
|
}
|
|
|
|
public class GooglePlacesResponse
|
|
{
|
|
public Candidate[]? Candidates { get; set; }
|
|
}
|
|
|
|
public class Candidate
|
|
{
|
|
public string? Formatted_Address { get; set; }
|
|
public string? Name { get; set; }
|
|
}
|
|
|
|
public class GoogleDirectionsResponse
|
|
{
|
|
public Route[]? Routes { get; set; }
|
|
}
|
|
|
|
public class Route
|
|
{
|
|
public Leg[]? Legs { get; set; }
|
|
}
|
|
|
|
public class Leg
|
|
{
|
|
public TextValue? Distance { get; set; }
|
|
public TextValue? Duration { get; set; }
|
|
public TextValue? DurationInTraffic { get; set; }
|
|
}
|
|
|
|
public class TextValue
|
|
{
|
|
public string Text { get; set; }
|
|
}
|
|
} |