将地图别针保存到数组,Swift

时间:2016-03-18 14:46:29

标签: ios swift core-data nsuserdefaults mkpointannotation

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

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

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

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

1 个答案:

答案 0 :(得分:1)

如果您想在应用程序启动之间保留数据,并且您没有存储太多数据,那么最简单的方法就是使用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()
    }