大家好,我是Xcode和Swift的新手。
我正在编写一个应用程序,用户可以从列表中选择一个国家,然后将其添加到地图上。
基本上,这只是一个带有标记的地图。
我在哪里可以获得默认国家列表?例如下拉菜单之类的东西,这样就不需要自己硬编码所有国家了。
接下来,我知道下一个问题很大,所以我只期望得到一些指导:
有人能否给我指示如何使用所选国家的GPS坐标,以便将其放置在地图上?
非常感谢大家的帮助。
大家好,我是Xcode和Swift的新手。
我正在编写一个应用程序,用户可以从列表中选择一个国家,然后将其添加到地图上。
基本上,这只是一个带有标记的地图。
我在哪里可以获得默认国家列表?例如下拉菜单之类的东西,这样就不需要自己硬编码所有国家了。
接下来,我知道下一个问题很大,所以我只期望得到一些指导:
有人能否给我指示如何使用所选国家的GPS坐标,以便将其放置在地图上?
非常感谢大家的帮助。
试试这个。
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
}
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
}
}];
// 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
}
}