Skip to content

Instantly share code, notes, and snippets.

@pictos
Created November 23, 2017 14:42
Show Gist options
  • Save pictos/5bdad9a3f57d95f24b837268a2c5fd3e to your computer and use it in GitHub Desktop.
Save pictos/5bdad9a3f57d95f24b837268a2c5fd3e to your computer and use it in GitHub Desktop.
Serviço para usar o DirectionsAPI
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