Created
November 23, 2017 14:42
-
-
Save pictos/5bdad9a3f57d95f24b837268a2c5fd3e to your computer and use it in GitHub Desktop.
Serviço para usar o DirectionsAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using RouteMap.Model; | |
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
namespace RouteMap.Services | |
{ | |
public class DirecaoServico | |
{ | |
private readonly string baseUrl = "https://maps.googleapis.com/maps/api/directions/"; | |
private HttpClient _httpClient; | |
private readonly string _key; | |
//Coloque a key na MapaViewModel. Adquira a key neste site https://developers.google.com/maps/documentation/directions/?hl=pt-br | |
public DirecaoServico(string key) => _key = key; | |
public async Task<DirecaoResposta> ObterRota(string origem, string destino) | |
{ | |
_httpClient = new HttpClient(); | |
string url = string.Format(baseUrl + $"json?origin={origem}&destination={destino}&key={_key}"); | |
var resposta = await _httpClient.GetAsync(url); | |
var json = await resposta.Content.ReadAsStringAsync(); | |
if (resposta.IsSuccessStatusCode) | |
{ | |
var resultado = JsonConvert.DeserializeObject<DirecaoResposta>(json); | |
return resultado; | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment