Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Get user country?

Hi,

Is it possible to get the users country without performing a geocode request?

In my app I want to have some default settings if the users is from the us (imperial system) and if from any other country I want to use the metric system as default.

It would be great if I could know the region format the phone (iphone) is set to or something like that. If not I guess I could always perform a geo request to determine the country.

— asked October 22nd 2010 by Magnus Ottosson
  • contry
  • locale
  • region
0 Comments

3 Answers

  • I'm looking for the same thing. There is a way to do it on the iPhone in object-c so hopefully if not already, the Appcelerator team will look into an implementation. I'm guessing there is a similar way on the Android.

    I realize I'm posting Objective-C, but just in case an Appcelerator developer decides to implement it, I'll put a couple ways I've seen to do it….

    Method 1:

    NSLocale *locale = [NSLocale currentLocale];
    NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
    
    NSString *countryName = [locale displayNameForKey: NSLocaleCountryCode
                             value: countryCode];
    

    Method 2: (iOS 4+)

    #import <CoreTelephony/CTTelephonyNetworkInfo.h>
    #import <CoreTelephony/CTCarrier.h>
    
    CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
    CTCarrier *carrier = [netInfo subscriberCellularProvider];
    NSString *mcc = [carrier mobileCountryCode];
    
    — answered November 2nd 2010 by Steven Day
    permalink
    1 Comment
    • Sweet, I hope this will be implemented in the Appcelerator API.

      — commented November 2nd 2010 by Magnus Ottosson
  • Maybe you can open a hidden webview including MaxMind JS api:

    http://www.maxmind.com/app/javascript

    Example:
    Country Code: TR
    Country Name: Turkey

    Source code:

    <script language="JavaScript" src="http://j.maxmind.com/app/country.js"></script>
    <br>Country Code:
    <script language="JavaScript">document.write(geoip_country_code());</script>
    <br>Country Name:
    <script language="JavaScript">document.write(geoip_country_name());</script>
    
    — answered October 25th 2010 by Musa Ulker
    permalink
    0 Comments
  • I was hoping to be able to do this without the need to perform a web request. If I need to do this I would use the google geocoding service which turns a lat,lng from the gps into a location: http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true

    I guess this might be the way to go for me.

    — answered October 25th 2010 by Magnus Ottosson
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.