Saving map pins to array, Swift

2019-06-01 20:32发布

问题:

I am creating a map type application, once the user presses a button a pin is dropped to the map at their current location. I am trying to save map pins to an array so that they remain once the app is closed.

Here is my code so far:

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

How can I save the pin information to an array which is reloaded each time the app is opened?

回答1:

If you want to persist data between application launches, and you are not storing much data, the easy way is to use NSUserDefaults. You can do this by saying something like:

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

You will then need to read out those locations when the app relaunches. You can do that in viewDidLoad. For example:

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

If you want to remove all of the stored locations you can say something like:

    func removeStoredLocations(){
     NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
     NSUserDefaults.standardUserDefaults().synchronize()
    }