2

我正在创建一个地图类型的应用程序,一旦用户按下一个按钮,一个图钉就会被放到他们当前位置的地图上。我正在尝试将地图图钉保存到一个数组中,以便在应用程序关闭后它们仍然存在。

到目前为止,这是我的代码:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last
    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.004, longitudeDelta: 0.004))
    self.placesMap?.setRegion(region, animated: true)
    self.locationManager.stopUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
    print("Error code: " + error.localizedDescription)
}

// Add button action
@IBAction func addButton(sender: AnyObject) {
    let annotation = MKPointAnnotation()
    annotation.coordinate = CLLocationCoordinate2D(latitude: self.placesMap.userLocation.coordinate.latitude, longitude: self.placesMap.userLocation.coordinate.longitude)
    self.placesMap.addAnnotation(annotation)
    self.locationManager.startUpdatingLocation()
}

如何将 pin 信息保存到每次打开应用程序时重新加载的数组?

4

1 回答 1

2

如果您想在应用程序启动之间保留数据,并且您没有存储太多数据,那么简单的方法是使用 NSUserDefaults。你可以这样说:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last
    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.004, longitudeDelta: 0.004))
    self.placesMap?.setRegion(region, animated: true)
    self.locationManager.stopUpdatingLocation()



     let locationDictionary:[String:Double] = ["latitude":center.latitude,"longitude":center.longitude]
     var locationArray = [[String:Double]]()
     if NSUserDefaults.standardUserDefaults().objectForKey("locationArray") != nil {
        locationArray = NSUserDefaults.standardUserDefaults().objectForKey("locationArray") as! [[String:Double]]

    }

    locationArray.append(locationDictionary)

    NSUserDefaults.standardUserDefaults().setObject(locationArray, forKey: "locationArray")
    NSUserDefaults.standardUserDefaults().synchronize()
    }

然后,您需要在应用重新启动时读出这些位置。您可以在 viewDidLoad 中执行此操作。例如:

override func viewDidLoad(){
    super.viewDidLoad()
    if NSUserDefaults.standardUserDefaults().objectForKey("locationArray") != nil {
    for dictionary in NSUserDefaults.standardUserDefaults().objectForKey("locationArray") as! [[String:Double]]{
        let center = CLLocationCoordinate2D(latitude: dictionary["latitude"]!, longitude: dictionary["longitude"]!)
        let annotation = MKPointAnnotation()
        annotation.coordinate = center
        self.placesMap.addAnnotation(annotation)
    }
  }
}

如果要删除所有存储的位置,可以这样说:

    func removeStoredLocations(){
     NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
     NSUserDefaults.standardUserDefaults().synchronize()
    }
于 2016-03-18T15:26:47.217 回答