Swift和Xcode:如何拥有默认的国家列表?

3

大家好,我是Xcode和Swift的新手。

我正在编写一个应用程序,用户可以从列表中选择一个国家,然后将其添加到地图上。

基本上,这只是一个带有标记的地图。

我在哪里可以获得默认国家列表?例如下拉菜单之类的东西,这样就不需要自己硬编码所有国家了。

接下来,我知道下一个问题很大,所以我只期望得到一些指导:

有人能否给我指示如何使用所选国家的GPS坐标,以便将其放置在地图上?

非常感谢大家的帮助。


你学过Google Places API吗? - Andrey
关于一个国家及其坐标的列表:http://gis.stackexchange.com/questions/71921/list-of-central-coordinates-for-all-countries - Robert
1
关于向地图添加标记:https://dev59.com/AWkw5IYBdhLWcg3wbqKZ - Robert
这段代码将为您获取所有国家的列表 - 但是您需要设置一个硬编码字典来存储它们的GPS中心...让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
谢谢大家和@Robert提供的链接!我将使用http://www.geonames.org API,它包括经纬度和国家列表! - Antoine
4个回答

10

试试这个。

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
}

有趣,但它给出的是语言名称,而不是国家名称。 - Eric Aya
1
let country = locale.displayName(forKey: NSLocaleCountryCode, value: countryCode) # 没有问题,替换 - Jamil
欢迎。当您需要替换语言名称时,请使用'NSLocaleCountryCode' 'NSLocaleIdentifier'。 - Jamil
完美,它对我很有效!谢谢,我从你的代码中学到了很多。 - Antoine

4
使用由苹果提供的CLGeocoder类,您可以同时解决这两个问题。只需要求用户在UITextField或其他地方输入国家名称,然后您就可以使用该字符串查找相关地点的名称和坐标,方法如下所示:

将地名转换为坐标

使用简单字符串和CLGeocoder类来启动正向地理编码请求。没有指定字符串请求的格式:分隔符字符是可选的,但不是必需的,并且地理编码服务器将该字符串视为不区分大小写。例如,以下任何一个字符串都会产生结果:

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
目前为止,我只是想弄清楚如何获取一个国家的经纬度列表,而不必硬编码,我找到了完美的geonames API。您如何将用户输入的位置集成到CLGeocoder中? - Antoine
1
我在Github上为您找到了这个。请探索此代码:https://github.com/lminhtm/LMGeocoder - Nishant
谢谢Nishant提供的所有信息,我会仔细阅读并听取你的建议。 - Antoine

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) 
    }
 }

0
作为可重用的 Swift 4 扩展:
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
   }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接