Ios Swift和Xcode:如何拥有默认的国家列表?
嗨,我是Xcode和Swift的新手 我正在编写一个应用程序,用户可以从列表中选择一个国家,以便在地图上添加pin码 基本上,它只是一张有图钉的地图 我在哪里可以得到违约国家的列表?比如下拉列表之类的,这样我就不需要自己硬编码所有的国家 然后,我知道下一个问题很重要,所以我希望得到一些指导: 有人能告诉我如何使用选定国家的GPS坐标,以便在地图上放置pin码吗Ios Swift和Xcode:如何拥有默认的国家列表?,ios,swift,Ios,Swift,嗨,我是Xcode和Swift的新手 我正在编写一个应用程序,用户可以从列表中选择一个国家,以便在地图上添加pin码 基本上,它只是一张有图钉的地图 我在哪里可以得到违约国家的列表?比如下拉列表之类的,这样我就不需要自己硬编码所有的国家 然后,我知道下一个问题很重要,所以我希望得到一些指导: 有人能告诉我如何使用选定国家的GPS坐标,以便在地图上放置pin码吗 谢谢大家的帮助。使用苹果提供的CLGeocoder类,您可以同时解决这两个问题。只需让用户在UITextField或其他内容中输入国家名
谢谢大家的帮助。使用苹果提供的
CLGeocoder
类,您可以同时解决这两个问题。只需让用户在UITextField
或其他内容中输入国家名称,您就可以使用该字符串使用下面代码中说明的地理编码方法查找相关地点的名称和坐标:
将地名转换为坐标
使用带有简单字符串的CLGeocoder
类来启动前向地理编码请求。没有
基于字符串的请求的指定格式:分隔符字符为
欢迎,但不是必需的,geocoder服务器会处理字符串
不区分大小写。例如,以下任何字符串都将
产量结果:
试试这个
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
}
作为可重复使用的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
}
}
你了解Google Places API吗?关于国家及其坐标的列表:关于在地图上添加PIN:此代码将为你提供所有国家的列表-但你必须建立一个硬编码字典,其中包含他们的GPS中心。。。让locale=NSLocale.currentLocale()让countryArray=NSLocale.ISOCountryCodes()变量unsortedCountryArray:[字符串]=[]用于countryArray中的countryCode{let displaynamString=locale.displayNameForKey(NSLocaleCountryCode,值:countryCode),如果displayNameString!=nil{unsortedCountryArray.append(displayNameString!)}让sortedCountryArray=已排序(unsortedCountryArray,感谢各位和@Robert的链接!我将使用API,其中包括long和lat以及国家列表!您使用的是apple map还是google map?您需要阅读一些文档。没有,到目前为止,我只是想弄清楚如何在不必硬编码的情况下获得long和lat国家列表,我得到了地理信息names API非常完美。如何在CLGeocoder中集成用户输入位置?我在github上为您找到了此代码。请探索此代码:感谢Nishant提供我将通读并听取您的建议的所有信息有趣,但它提供了语言名,而不是国家名。没有问题,请替换为let country=locale.displayNameForKey(NSLocaleCountryCode,value:countryCode)欢迎您。当您需要语言名称重播时,请参阅“NSLocaleCountryCode”“NSLocaleIdentifier”完美,它适合我!谢谢您,我从您的代码中学到了很多
// 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)
}
}
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
}
}