Get Country/Region name from windows phone 8
Asked Answered
A

5

9

I have the screenshot

enter image description here

and I am intending to retrieve the country which should be "Nigeria". After going through the System.Globalization class, I found the code snippet below

System.Globalization.RegionInfo.CurrentRegion.EnglishName

But I am getting "United States", which is the "Regional Format" from the image above. Is there no way i can retrieve the Country/Region value settings?

Audible answered 16/7, 2013 at 11:16 Comment(5)
What do you get with RegionInfo.CurrentRegion.DisplayName?Nuggar
I get the same result "United States" using RegionInfo.CurrentRegion.DisplayNameAudible
Have you tried testing on actual device?Jermainejerman
i have tried his code on device but he is correct there is property which will reflect the correct country/region ..Cwm
I have tried it on an actual device, still does not give me "Nigeria"Audible
A
-2

This is what i did at the end of the day. First I used the Location API library to get the coordinates(longitude and latitude) of the location

        Geolocator geolocator = new Geolocator();
        geolocator.DesiredAccuracyInMeters = 50;

        try
        {
            // Request the current position
            Geoposition geoposition = await geolocator.GetGeopositionAsync(
                maximumAge: TimeSpan.FromMinutes(5),
                timeout: TimeSpan.FromSeconds(10)
            );

            string latitude = geoposition.Coordinate.Latitude.ToString("0.00");
            string longitude = geoposition.Coordinate.Longitude.ToString("0.00");                 


        }
        catch (Exception ex)
        {
            if ((uint)ex.HResult == 0x80004004)
            {
                // the application does not have the right capability or the location master switch is off
                MessageBox.Show("Location is disabled in phone settings.", "Can't Detect Location", MessageBoxButton.OK);

            }
            //else
            {
                // something else happened acquring the location
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }

then using google's reverse geolocation to get the information or details of the location based on the longitude and latitude

http://maps.googleapis.com/maps/api/geocode/json?latlng=latitiude,longitude&sensor=true

i.e if the latitude is 60 and the longitude is 60

http://maps.googleapis.com/maps/api/geocode/json?latlng=60,60&sensor=true

From the json result, you will be able to retrieve the country's long and short name.

Audible answered 31/7, 2013 at 21:3 Comment(2)
Damn, that is such a nasty solution. What will happen when someone goes to another country? Then all of a sudden the formatting in his phone is changed lol.Shaylashaylah
Get your point Leon, wish Microsoft will come up with a solution.Audible
C
2

I know it's old, but maybe someone comes here looking for answer:

System.Globalization.RegionInfo.CurrentRegion.TwoLetterISORegionName
Cari answered 12/6, 2014 at 21:46 Comment(1)
This only returns the two letter code, e.g. US rather than United States. Also, the question seems to be about why the OP is not getting the name he expected.Dysthymia
P
0

Use System.Threading.Thread.CurrentThread.CurrentCulture. It should correctly reflect the phone language.

Peterpeterborough answered 16/7, 2013 at 12:49 Comment(2)
I think he wants to get the region, not the language.Hertford
I have tried System.Threading.Thread.CurrentThread.CurrentCulture, this will give me the locale-country code which was en-US. What i want to get is Country/Region value setting which is "Nigeria" from the image i posted above.Audible
B
0

Try this

System.Globalization.RegionInfo.CurrentRegion.DisplayName;
Bakelite answered 16/7, 2013 at 20:43 Comment(1)
Still shows "United States", I think the RegionInfo class only shows the Regional Format values not the Country/Region valueAudible
C
0

The correct answer to what the user is asking is this:

Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion

This should give you information regarding the Country/Region selection in the language+region setting.

Crisper answered 4/1, 2016 at 12:27 Comment(0)
A
-2

This is what i did at the end of the day. First I used the Location API library to get the coordinates(longitude and latitude) of the location

        Geolocator geolocator = new Geolocator();
        geolocator.DesiredAccuracyInMeters = 50;

        try
        {
            // Request the current position
            Geoposition geoposition = await geolocator.GetGeopositionAsync(
                maximumAge: TimeSpan.FromMinutes(5),
                timeout: TimeSpan.FromSeconds(10)
            );

            string latitude = geoposition.Coordinate.Latitude.ToString("0.00");
            string longitude = geoposition.Coordinate.Longitude.ToString("0.00");                 


        }
        catch (Exception ex)
        {
            if ((uint)ex.HResult == 0x80004004)
            {
                // the application does not have the right capability or the location master switch is off
                MessageBox.Show("Location is disabled in phone settings.", "Can't Detect Location", MessageBoxButton.OK);

            }
            //else
            {
                // something else happened acquring the location
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }

then using google's reverse geolocation to get the information or details of the location based on the longitude and latitude

http://maps.googleapis.com/maps/api/geocode/json?latlng=latitiude,longitude&sensor=true

i.e if the latitude is 60 and the longitude is 60

http://maps.googleapis.com/maps/api/geocode/json?latlng=60,60&sensor=true

From the json result, you will be able to retrieve the country's long and short name.

Audible answered 31/7, 2013 at 21:3 Comment(2)
Damn, that is such a nasty solution. What will happen when someone goes to another country? Then all of a sudden the formatting in his phone is changed lol.Shaylashaylah
Get your point Leon, wish Microsoft will come up with a solution.Audible

© 2022 - 2024 — McMap. All rights reserved.