【问题标题】:How to Force Default Language to English in CLLocationManager?如何在 CLLocationManager 中强制默认语言为英语?
【发布时间】:2016-11-07 16:16:05
【问题描述】:

所以,我是 swift 新手,我将当前的经纬度转换为城市名称和国家/地区,它可以正常工作:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    if didFindLocation == false
    {
        didFindLocation = true
        locationManager.stopUpdatingLocation()
        userLocation = locations[0]
        long = userLocation.coordinate.longitude;
        lat = userLocation.coordinate.latitude;
        print("\(lat),\(long)")
        converLocationToCity()
    }
}

func converLocationToCity()
{
    let geoCoder = CLGeocoder()
    userLocation = CLLocation(latitude: self.lat, longitude: self.long)
    geoCoder.reverseGeocodeLocation(userLocation, completionHandler:
    {
        (placemarks, error) -> Void in
        var placeMark: CLPlacemark!
        placeMark = placemarks?[0]
        if let city = placeMark.addressDictionary!["State"] as? String
        {
            self.city = city as String
        } else
        {
            self.city = ""
        }
        if let country = placeMark.addressDictionary!["Country"] as? String
        {
            self.country = country as String
        } else
        {
            self.country = ""
        }
        self.currentCity.name = ("\(self.city), \(self.country)" as String)
        print("\(self.currentCity.name)")
        self.fetchWeather.performCurrentWeatherFetch(forSelectedCity: self.currentCity.name)
        DispatchQueue.main.async()
            {
            (self.superview as! UICollectionView).reloadData()
        }
    })
}

但是当设备设置为其他语言时,例如俄语,它会以俄语字符返回城市名称和国家,但我需要它只是英文,请任何人提供一些想法或建议?谢谢!

【问题讨论】:

  • mm 这似乎是我所需要的,但它在 Objective 中,而且我是编程和 swift 方面的新手,所以我已经尝试了 40 分钟但无法管理它......

标签: ios swift3 cllocationmanager clplacemark


【解决方案1】:

这是我的解决方案 在获取位置数据时,我更改了 `UserDefaults.standard.set(["base"], forKey: "AppleLanguages")'

一旦我收到英文字典,我就会删除对象

UserDefaults.standard.removeObject(forKey: "AppleLanguages")

然后将applelanguage设置为默认值

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation:CLLocation = locations[0] as CLLocation
    // Call stopUpdatingLocation() to stop listening for location updates,
    // other wise this function will be called every time when user location changes.

    //  manager.stopUpdatingLocation()
    print("user latitude = \(userLocation.coordinate.latitude)")
    print("user longitude = \(userLocation.coordinate.longitude)")

    let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
    //location.accessibilityLanguage = "en-US"
    UserDefaults.standard.set(["base"], forKey: "AppleLanguages")
    geoCoder.reverseGeocodeLocation(location, completionHandler: { placemarks, error in
        guard let addressDict = placemarks?[0].addressDictionary else {
            return
        }
        print(addressDict)
        // Print each key-value pair in a new row
        addressDict.forEach { print($0) }

        // Print fully formatted address
        if let formattedAddress = addressDict["FormattedAddressLines"] as? [String] {
            print(formattedAddress.joined(separator: ", "))
        }

        // Access each element manually
        if let locationName = addressDict["Name"] as? String {
            print(locationName)
        }
        if let street = addressDict["Thoroughfare"] as? String {
            print(street)
        }
        var myCity:String = ""
        if let city = addressDict["City"] as? String {
            print(city)
            if(city != "" ){
                myCity = city
            }
        }
        if let zip = addressDict["ZIP"] as? String {
            print(zip)
        }
        var myCountry:String = ""
        if let country = addressDict["Country"] as? String {
            print(country)
            if(country != "" ){
                myCountry = country
            }
            MyGenericFunctions.sharedInstance.saveCountry(country: country)
        }
        manager.stopUpdatingLocation()

        if(myCity != "" && myCountry != "" && self.isCurrLocAPICalled != true){
           print("API Called")
            self.isCurrLocAPICalled = true
            self.callLocationSearch(strCity: myCity, strCountry: myCountry)
            UserDefaults.standard.removeObject(forKey: "AppleLanguages")
        }

    })

    //manager.stopUpdatingLocation()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 2022-06-23
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    相关资源
    最近更新 更多