예제 #1
0
        private async Task <GeoObj> GetRequest(string arg)
        {
            /*Google wants a '+' beween arguments in it's API
             * The line below uses a regular expression to replace any spaces with a '+' */
            arg = Regex.Replace(arg, "\\s", "+");

            var client = new HttpClient();

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var response = await client.GetAsync($"{BASE_URL}{arg}&key={_apiKey}");

            response.EnsureSuccessStatusCode(); //throws an expection if there was an error

            var geo = new GeoObj();

            //Deserializes the JSON
            JsonConvert.PopulateObject(await response.Content.ReadAsStringAsync(), geo);

            if (geo.status == "OK")
            {
                return(geo);
            }
            else //API returned an error message
            {
                throw new Exception("Server returned status: " + geo.status);
            }
        }
예제 #2
0
        internal Address(GeoObj obj)
        {
            Street = obj.results[0].address_components
                     .Where(x => x.types[0] == "street_number")
                     .DefaultIfEmpty(new Address_Components()
            {
                long_name = null
            })
                     .SingleOrDefault().long_name;

            Street += " " + obj.results[0].address_components
                      .Where(x => x.types[0] == "route")
                      .DefaultIfEmpty(new Address_Components()
            {
                long_name = null
            })
                      .SingleOrDefault().long_name;

            Apt = obj.results[0].address_components
                  .Where(x => x.types[0] == "premise")
                  .DefaultIfEmpty(new Address_Components()
            {
                long_name = null
            })
                  .SingleOrDefault().long_name;

            City = obj.results[0].address_components
                   .Where(x => x.types[0] == "locality")
                   .DefaultIfEmpty(new Address_Components()
            {
                long_name = null
            })
                   .SingleOrDefault().long_name;

            Region = obj.results[0].address_components
                     .Where(x => x.types[0] == "administrative_area_level_1")
                     .DefaultIfEmpty(new Address_Components()
            {
                long_name = null
            })
                     .SingleOrDefault().long_name;

            PostalCode = obj.results[0].address_components
                         .Where(x => x.types[0] == "postal_code")
                         .DefaultIfEmpty(new Address_Components()
            {
                long_name = null
            })
                         .SingleOrDefault().long_name;

            Country = obj.results[0].address_components
                      .Where(x => x.types[0] == "country")
                      .DefaultIfEmpty(new Address_Components()
            {
                long_name = null
            })
                      .SingleOrDefault().long_name;
        }
 internal MapLocation(GeoObj objs) : base(objs)
 {
     latitude  = objs.results[0].geometry.location.lat;
     longitude = objs.results[0].geometry.location.lng;
 }