Пример #1
0
        public async Task <CoordinateServiceResult> Lookup(string locationName)
        {
            var result = new CoordinateServiceResult();
            //{
            //    Success = false,
            //    Message = "Failed to look up the Location"
            //};

            var bingKey         = Startup.configuration["AppSettings:MapKey"];
            var encodedLocation = WebUtility.UrlEncode(locationName);
            var Url             = $"http://dev.virtualearth.net/REST/v1/Locations?query={encodedLocation}&key={bingKey}";

            var    httpClinet = new HttpClient();
            string webServiceMessage;
            JToken resources;
            string Json = await httpClinet.GetStringAsync(Url);

            if (EnsureCoordinateReceived(Json, out webServiceMessage, out resources, encodedLocation))
            {
                SetCoordinateValues(resources, result);
            }
            else
            {
                result.Message = webServiceMessage;
            }

            return(result);
        }
Пример #2
0
        private void SetCoordinateValues(JToken resources, CoordinateServiceResult result)
        {
            var coords = resources[0]["geocodePoints"][0]["coordinates"];

            result.Latitude  = (double)coords[0];
            result.Longitude = (double)coords[1];
            result.Success   = true;
            result.Message   = "Success";
        }