3

I am trying to give users the option to save their current location. I use the defaults to save it and I found out, how to get the users location. But I am not sure how to combine those two tasks. So here is my question:

How can I get my function to call users location, so it can be saved?

@IBAction func addLocation(_ sender: Any) {

// CALL LOCATION MANAGER TO GET LOCATION IN LAT AND LONG    
    self.saveDefaults()

    }

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
    print("locations = \(locValue.latitude) \(locValue.longitude)")
}

func saveDefaults()
{
    UserDefaults.standard.set(self.pickers, forKey: "pickers")
    print("SAVED PICKERS: \(pickers)")

}

This is my most recent try but I don't know why my struct (lat: ,long:) is not taking the input

@IBAction func addLocation(_ sender: Any) {

    var locations: [CLLocation]
    let manager: CLLocationManager

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

    self.pickers.append(pickerStruct(lat: userLocation.coordinate.latitude, long: userLocation.coordinate.longitude))

    self.saveDefaults()

    }
Marcy
  • 4,611
  • 2
  • 34
  • 52
Roman H
  • 251
  • 2
  • 10

2 Answers2

7
@IBAction func addLocation(_ sender: Any) {
    locationManager.startUpdatingLocation() //This will call the delegate method below where you can save the location
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    guard locations.count > 0 else {
        return
    }

    guard let location = locations.last else {
        return
    }

    locationManager.stopUpdatingLocation() //Stop location updates after getting the location and save that location as below

    let encodedLocation = NSKeyedArchiver.archivedData(withRootObject: location)
    UserDefaults.standard.set(encodedLocation, forKey: "savedLocation")

}

To get back the location from UserDefaults:

let previousLocationEncoded = UserDefaults.standard.object(forKey: "savedLocation") as? Data
let previousLocationDecoded = NSKeyedUnarchiver.unarchiveObject(with: previousLocationEncoded!) as! CLLocation
Sujal
  • 1,447
  • 19
  • 34
  • I am not sure if you got me right. I want to save the location when a user wants it to (when he starts an action pressing on the button). When I understand you right it will save it every time the location changed, right? – Roman H Feb 12 '19 at 08:27
  • @Roman H I have updated the answer as per your requirement :) – Sujal Feb 12 '19 at 08:34
3

The functions used in Sujal's answer are deprecated. Here are the current (2021) working version of the functions:

// Save location to UserDefaults
if let encodedLocation = try? NSKeyedArchiver.archivedData(withRootObject: location, requiringSecureCoding: false) {
    UserDefaults.standard.set(encodedLocation, forKey: "savedLocation")
}

// Load the last saved location
if let loadedLocation = UserDefaults.standard.data(forKey: "savedLocation"),
   let decodedLocation = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(loadedLocation) as? CLLocation {
    // Use the decodedLocation as you want
}
Zoltan Vinkler
  • 1,207
  • 1
  • 15
  • 20