public async Task <GeoCoordsResults> GetCoordsAsync(string name)
        {
            var result = new GeoCoordsResults()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            try
            {
                var apiKey      = _config["Keys:BingKey"]; // Pulled from environment variable (Keys__BingKey).
                var encodedName = WebUtility.UrlEncode(name);

                var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={apiKey}";

                var client = new HttpClient();
                var json   = await client.GetStringAsync(url);


                // github.com/shawnwildermuth/BuildingWebASPNETCore
                // Read out the results
                // Fragile, might need to change if the Bing API changes
                var results   = JObject.Parse(json);
                var resources = results["resourceSets"][0]["resources"];
                if (!resources.HasValues)
                {
                    result.Message = $"Could not find '{name}' as a location";
                }
                else
                {
                    var confidence = (string)resources[0]["confidence"];
                    if (confidence != "High")
                    {
                        result.Message = $"Could not find a confident match for '{name}' as a location";
                    }
                    else
                    {
                        var coords = resources[0]["geocodePoints"][0]["coordinates"];
                        result.Latitude  = (double)coords[0];
                        result.Longitude = (double)coords[1];
                        result.Success   = true;
                        result.Message   = "Success";
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
            }

            return(result);
        }
        public async Task <GeoCoordsResults> GetCoordsAsync(string name)
        {
            var result = new GeoCoordsResults()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            //Best Practice would be to use evironment variable instead of config file
            var apiKey      = _config["Keys:BingMapKey"];
            var encodedName = WebUtility.UrlEncode(name);
            var url         = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={apiKey}";

            var client = new HttpClient();

            var json = await client.GetStringAsync(url);

            // Read out the results
            // Fragile, might need to change if the Bing API changes
            var results   = JObject.Parse(json);
            var resources = results["resourceSets"][0]["resources"];

            if (!results["resourceSets"][0]["resources"].HasValues)
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                var confidence = (string)resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for '{name}' as a location";
                }
                else
                {
                    var coords = resources[0]["geocodePoints"][0]["coordinates"];
                    result.Latitude  = (double)coords[0];
                    result.Longitude = (double)coords[1];
                    result.Success   = true;
                    result.Message   = "Success";
                }
            }
            return(result);
        }
Ejemplo n.º 3
0
        public async Task <GeoCoordsResults> GetCoordsAsync(string name = "Warszawa")
        {
            var result = new GeoCoordsResults()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            var apiKey      = _config["Keys:BingKey"]; //"AiUaIhWwTJ8_ESxX0ZyXYHsjF51pni-Oiar0ElcyIYLO6ayqiuNxf6xQN97od3sA"; //_config["Keys:BingKey"];
            var encodedName = WebUtility.UrlEncode(name);
            var url         = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={apiKey}";

            HttpClient client = new HttpClient();

            string json = await client.GetStringAsync(url);

            // Read out the results
            // Fragile, might need to change if the Bing API changes
            JObject results   = JObject.Parse(json);
            JToken  resources = results["resourceSets"][0]["resources"];

            if (!resources.HasValues)
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                string confidence = (string)resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for '{name}' as a location";
                }
                else
                {
                    JToken coords = resources[0]["geocodePoints"][0]["coordinates"];
                    result.Latitude  = (double)coords[0];
                    result.Longitude = (double)coords[1];
                    result.Success   = true;
                    result.Message   = "Success";
                }
            }

            return(result);
        }