3

Hi I am new to Xcode and Swift.

I am tying to code an App where user can select a country from a list so that it adds a pin to a map.

Basically, it is just a map with pins.

Where could I get a list of default countries ? like a drop-down or something so that I do not need to hardcode all of the countries myself.

Then, I know the next question is big so I just expect some guidance:

Could someone give me any directions on how using the GPS coordinates of the country selected so that it places a pin to a map ?

Thanks you all for your help.

Wain
  • 118,658
  • 15
  • 128
  • 151
Antoine
  • 559
  • 8
  • 21
  • Have you learned about Google Places API ? – Andrey Sep 15 '15 at 10:51
  • Regarding a list of countries and their coordinates: http://gis.stackexchange.com/questions/71921/list-of-central-coordinates-for-all-countries – Robert Sep 15 '15 at 10:54
  • 1
    Regarding adding pins to a map: http://stackoverflow.com/questions/9891926/quickly-adding-single-pin-to-mkmapview – Robert Sep 15 '15 at 10:54
  • This code will get you a list of all countries - but you'll have to set up a hardcoded dictionary with their GPS centers... let locale = NSLocale.currentLocale() let countryArray = NSLocale.ISOCountryCodes() var unsortedCountryArray:[String] = [] for countryCode in countryArray { let displayNameString = locale.displayNameForKey(NSLocaleCountryCode, value: countryCode) if displayNameString != nil { unsortedCountryArray.append(displayNameString!) } } let sortedCountryArray = sorted(unsortedCountryArray, <) – Woodstock Sep 15 '15 at 10:57
  • Thanks guys and @Robert for the link ! I will use the http://www.geonames.org API it includes long and lat plus list of countries ! – Antoine Sep 15 '15 at 11:53

4 Answers4

10

Try with this.

func counrtyNames() -> NSArray{

    var countryCodes = NSLocale.ISOCountryCodes()
    var countries:NSMutableArray = NSMutableArray()

    for countryCode  in countryCodes{
        let dictionary : NSDictionary = NSDictionary(object:countryCode, forKey:NSLocaleCountryCode)

        //get identifire of the counrty
        var identifier:NSString? = NSLocale.localeIdentifierFromComponents(dictionary as! [String : String])

        let locale = NSLocale.currentLocale()
        //get country name
        let country = locale.displayNameForKey(NSLocaleCountryCode, value : countryCode)//replace "NSLocaleIdentifier"  with "NSLocaleCountryCode" to get language name

        if country != nil {//check the country name is  not nil
            countries.addObject(country!)
        }
    }
    NSLog("\(countries)")
    return countries
}
Jamil
  • 2,977
  • 1
  • 13
  • 23
4

Using the CLGeocoder class provided by Apple, you can solve both your problems simultaneously. Just ask the user to input the country name in a UITextField or something and you can use that string to find names and coordinates of the related places using the Geocoding method explained in the code below:

Converting Place Names Into Coordinates

Use the CLGeocoder class with a simple string to initiate forward-geocoding requests. There is no designated format for string-based requests: Delimiter characters are welcome, but not required, and the geocoder server treats the string as case-insensitive. For example, any of the following strings would yield results:

CLGeocoder* geocoder = [[CLGeocoder alloc] init];

[geocoder geocodeAddressString:@"India"

 completionHandler:^(NSArray* placemarks, NSError* error){

     for (CLPlacemark* aPlacemark in placemarks)
     {
         // Process the placemark and place the pin on MKMapView
     }
}];
Nishant
  • 12,529
  • 9
  • 59
  • 94
  • Are you using apple map or google map? You will need to go through some documentations. – Nishant Sep 15 '15 at 11:55
  • None, so far I just wanted to figure how to get a list of countries with long and lat without having to hardcode and I got the geonames API for that that is perfect. How do you integrate the user input location in the CLGeocoder ? – Antoine Sep 15 '15 at 11:57
  • 1
    I found this for you on github. Please explore this code: https://github.com/lminhtm/LMGeocoder – Nishant Sep 15 '15 at 12:04
  • Thanks Nishant for all of the infos I will read through and listen to your advice – Antoine Sep 15 '15 at 17:35
0
// For swift 2.2

let englishUS = NSLocale(localeIdentifier: "en_US") 
// use "fr_FR" to display country names in french or use any other code

let countryCodes = NSLocale.ISOCountryCodes() //Has all country codes

for localeNameOfCountries in countryCodes {

   if let aValue = englishUS.displayNameForKey(NSLocaleIdentifier, value: localeNameOfCountries) {
  //displaNameForKey returns [String?] so we use if let to unwrap
      print(aValue) 
    }
 }
Suhaib
  • 2,031
  • 3
  • 24
  • 36
0

As a reusable Swift 4 extension:

extension Locale {

   public var counrtyNames: [String] {
      return Locale.counrtyNames(for: self)
   }

   public static func counrtyNames(for locale: Locale) -> [String] {
      let nsLocale = locale as NSLocale
      let result: [String] = NSLocale.isoCountryCodes.compactMap {
         return nsLocale.displayName(forKey: .countryCode, value: $0)
      }
      // Seems `isoCountryCodes` already sorted. So, we skip sorting.
      return result
   }
}
Vlad
  • 6,402
  • 1
  • 60
  • 74