1

SO, I am new to swift and I made the conversion from current Lat and Long to City name and Country, it works fine like that:

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

But when the device is set to other language, Russian for example it returns me the City Name and Country in Russian characters, but I need it to be only in english, please anybody some ideas or suggestions? Thank you!

  • 1
    http://stackoverflow.com/questions/25144508/reverse-geocoding-to-return-results-only-in-english – LC 웃 Nov 08 '16 at 01:39
  • mm it seems to be what I need, but it's in Objective and I am new in programming and swift, so I am trying for 40 mins already and can't manage it... – Dumitru Rogojinaru Nov 08 '16 at 08:45

1 Answers1

1

Here is My Solution While getting the location data i change `UserDefaults.standard.set(["base"], forKey: "AppleLanguages")'

and once I have received the dictionary in English i remove the Object

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

which then sets applelanguage to default value

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()
}
Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25