Exemplo n.º 1
0
        public async Task <GeocodeModel> CalculateRouteDestination(string address)
        {
            RestRequest request = new RestRequest($"Locations", Method.GET);

            request.AddParameter("q", address, ParameterType.QueryString);
            request.AddParameter("o", "json", ParameterType.QueryString);
            request.AddParameter("key", _bingConfig.Key, ParameterType.QueryString);

            var result = await _bingClient.ExecuteTaskAsync <string>(request);

            if (!result.IsSuccessful)
            {
                throw new Exception($"Failed to find address with error {result.StatusCode}");
            }

            string              json = result.Data;
            JObject             jobj = JsonConvert.DeserializeObject <JObject>(json);
            BingGeocodeResult   obj  = jobj.ToObject <BingGeocodeResult>();
            BingGeocodeResource geo  = obj.resourceSets[0].resources[0];

            return(new GeocodeModel()
            {
                location = new LocationModel()
                {
                    latitude = geo.point.coordinates[0],
                    longitude = geo.point.coordinates[1]
                },
                confidence = geo.confidence,
                formattedAddress = geo.address.formattedAddress
            });
        }
Exemplo n.º 2
0
        private async Task <BingGeocodeResource> Geocode(string address)
        {
            RestRequest request = new RestRequest($"Locations", Method.GET);

            request.AddParameter("q", address, ParameterType.QueryString);
            request.AddParameter("o", "json", ParameterType.QueryString);
            request.AddParameter("key", _config.BingKey, ParameterType.QueryString);

            var result = await _bingClient.ExecuteTaskAsync <string>(request);

            if (!result.IsSuccessful)
            {
                throw new Exception($"Failed to find address with error {result.StatusCode}");
            }

            string              json = result.Data;
            JObject             jobj = JsonConvert.DeserializeObject <JObject>(json);
            BingGeocodeResult   obj  = jobj.ToObject <BingGeocodeResult>();
            BingGeocodeResource geo  = obj.resourceSets[0].resources[0];

            return(geo);
        }