【问题标题】:How do I save the current user location in defaults?如何将当前用户位置保存为默认值?
【发布时间】:2019-07-05 19:48:00
【问题描述】:

我正在尝试为用户提供保存当前位置的选项。我使用默认值来保存它,我发现了如何获取用户位置。但我不确定如何结合这两个任务。所以这是我的问题:

如何让我的函数调用用户位置,以便保存?

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

}

这是我最近的尝试,但我不知道为什么我的结构 (lat: ,long:) 没有接受输入

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

    }

【问题讨论】:

    标签: swift core-location userdefaults


    【解决方案1】:

    Sujal 的回答中使用的函数已被弃用。以下是功能的当前(2021 年)工作版本:

    // 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
    }
    

    【讨论】:

      【解决方案2】:
      @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")
      
      }
      

      从 UserDefaults 取回位置:

      let previousLocationEncoded = UserDefaults.standard.object(forKey: "savedLocation") as? Data
      let previousLocationDecoded = NSKeyedUnarchiver.unarchiveObject(with: previousLocationEncoded!) as! CLLocation
      

      【讨论】:

      • 我不确定你说得对不对。我想在用户想要的时候保存位置(当他开始按下按钮的动作时)。如果我理解正确,每次更改位置时都会保存它,对吗?
      • @Roman H 我已根据您的要求更新了答案:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 2021-07-28
      相关资源
      最近更新 更多