private static async Task SetCountryCode()

in Apps/Contoso.Forms.Puppet/Contoso.Forms.Puppet.UWP/App.xaml.cs [42:89]


        private static async Task SetCountryCode()
        {
            // The following country code is used only as a fallback for the main implementation.
            // This fallback country code does not reflect the physical device location, but rather the
            // country/region that corresponds to the culture it uses.
            var countryCode = new GeographicRegion().CodeTwoLetter;
            var accessStatus = await Geolocator.RequestAccessAsync();
            switch (accessStatus)
            {
                case GeolocationAccessStatus.Allowed:
                    var geoLocator = new Geolocator
                    {
                        DesiredAccuracyInMeters = 100
                    };
                    var position = await geoLocator.GetGeopositionAsync();
                    var myLocation = new BasicGeoposition
                    {
                        Longitude = position.Coordinate.Point.Position.Longitude,
                        Latitude = position.Coordinate.Point.Position.Latitude
                    };
                    var pointToReverseGeocode = new Geopoint(myLocation);
                    try
                    {
                        MapService.ServiceToken = Constants.BingMapsAuthKey;
                    }
                    catch (SEHException)
                    {
                        AppCenterLog.Info(LogTag, "Please provide a valid Bing Maps authentication key. For more info see: https://docs.microsoft.com/en-us/windows/uwp/maps-and-location/authentication-key");
                    }
                    var result = await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode);
                    if (result.Status != MapLocationFinderStatus.Success || result.Locations == null || result.Locations.Count == 0)
                    {
                        break;
                    }

                    // The returned country code is in 3-letter format (ISO 3166-1 alpha-3).
                    // Below we convert it to ISO 3166-1 alpha-2 (two letter).
                    var alpha3CountryCode = result.Locations[0].Address.CountryCode;
                    countryCode = new GeographicRegion(alpha3CountryCode).CodeTwoLetter;
                    break;
                case GeolocationAccessStatus.Denied:
                    AppCenterLog.Info(LogTag, "Geolocation access denied. In order to set country code in App Center, enable location service in Windows 10.");
                    break;
                case GeolocationAccessStatus.Unspecified:
                    break;
            }
            AppCenter.SetCountryCode(countryCode);
        }