コード例 #1
0
        public async Task <Localisation> ObtenirLocalisationAsync(GeocoderOptions options)
        {
            HttpClient client = new HttpClient();

            //Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            string       retourSvc;
            Localisation retour;

            var response = await client.GetAsync(ObtenirUri(options)).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                //TODO: Doit être traité et retourné dans un RouteCamionModel
                //Newtonsoft.Json.JO
                JObject retourJson = (JObject)JToken.Parse(response.Content.ReadAsStringAsync().Result).SelectToken("Response.View[*].Result[0].Location.NavigationPosition[*]");
                retour = new Localisation(retourJson.SelectToken("Latitude").Value <decimal>(),
                                          retourJson.SelectToken("Longitude").Value <decimal>()
                                          );
            }
            else
            {
                retour = null;
            }

            client.Dispose();
            return(retour);
        }
コード例 #2
0
ファイル: GeocoderService.cs プロジェクト: asanchezr/PSP
 /// <summary>
 /// Creates a new instance of a GeocoderService, initializes with specified arguments.
 /// </summary>
 /// <param name="options"></param>
 /// <param name="client"></param>
 public GeocoderService(IOptions <GeocoderOptions> options, IHttpRequestClient client)
 {
     this.Options = options.Value;
     this.Client  = client;
     if (!String.IsNullOrWhiteSpace(this.Options.Key))
     {
         client.Client.DefaultRequestHeaders.Add("apikey", this.Options.Key);
     }
 }
コード例 #3
0
        /// <summary>
        /// Creates a new instance of a GeoLocationConverter, initializes with specified arguments.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="service"></param>
        /// <param name="logger"></param>
        public GeoLocationConverter(IOptionsMonitor <GeocoderOptions> options, IGeocoderService service, ILogger <GeoLocationConverter> logger)
        {
            _options = options.CurrentValue;
            _service = service;
            _logger  = logger;

            ReadCacheIntoMemory();
            _writer = new StreamWriter(File.OpenWrite(_options.CacheFile));
        }
コード例 #4
0
        public async Task <IActionResult> GetAsync()
        {
            HttpClient client = new HttpClient();

            //Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            IActionResult retour = null;

            //TODO: Devrait être reçu en paramètre!
            RouteDistanceModel parametres = new RouteDistanceModel();

            //Obtenir le point Géolocalisé pour l'origine et la destination
            var geoOptions = new GeocoderOptions();

            geoOptions.AddAdresse(parametres.Depart);
            var origine = await _geoSrv.ObtenirLocalisationAsync(geoOptions).ConfigureAwait(false);

            geoOptions = new GeocoderOptions();
            geoOptions.AddAdresse(parametres.Destination);
            var destination = await _geoSrv.ObtenirLocalisationAsync(geoOptions).ConfigureAwait(false);

            //Construire les options de recherche
            var options = new RouteTruckOptions();

            options.AddLocalisation(origine, 0);
            options.AddLocalisation(destination, 1);
            options.AddMode(new Mode[] { Mode.fastest, Mode.truck, Mode.trafficDisabled });
            options.AddLimitedWeight(parametres.Poids);
            options.AddLength(parametres.Longueur);
            options.AddWidth(parametres.Largeur);

            //Obtenir la distance "Camion" entre 2 points géolocalisés
            try
            {
                var retourSrv = await _routeSrv.ObtenirDistanceAsync(options).ConfigureAwait(false);

                if (retourSrv == null)
                {
                    retour = BadRequest("Une erreur est survenue lors de l'appel du service");
                }
                else
                {
                    retour = Ok(retourSrv);
                }
            }
            catch (Exception)
            {
                retour = BadRequest("Une erreur est survenue lors de l'appel du service");
            }

            client.Dispose();
            return(retour);
        }
コード例 #5
0
        private Uri ObtenirUri(GeocoderOptions options)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            //TODO: À changer pour un string builder
            UriBuilder uri = new UriBuilder(Uri);

            uri.Query = string.Format("{2}&language=fr-ca&app_id={0}&app_code={1}", AppId, AppCode, options.ToString());

            return(uri.Uri);
        }