Exemple #1
0
        public async Task <ActionResult <CoordinatesResult> > GetCoordinatesForAddress(string address)
        {
            var plainTextAddress     = HttpUtility.UrlDecode(address);
            CoordinatesResult result = new CoordinatesResult();

            if (!String.IsNullOrWhiteSpace(address))
            {
                try
                {
                    var coords = _geoLocationProvider.GetLatLonFromAddressLocationIQ(plainTextAddress);

                    if (coords != null && coords.Longitude.HasValue && coords.Latitude.HasValue)
                    {
                        result.Latitude  = coords.Latitude;
                        result.Longitude = coords.Longitude;
                    }
                }
                catch
                {
                    return(NotFound());
                }
            }

            return(Ok(result));
        }
        public async Task <CoordinatesResult> Lookup(string location)
        {
            var result = new CoordinatesResult()
            {
                Success = false,
                Message = "Failure looking up coordinates"
            };

            var bingkey     = Startup.Configuration["AppSettings:BingKey"];
            var encodedName = WebUtility.UrlEncode(location);
            var url         = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={bingkey}";
            var client      = new HttpClient();

            var json = await client.GetStringAsync(url);

            // Read out the results - 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 location '{location}'";
            }
            else
            {
                var confidence = (string)resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for location '{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);
        }