0

我正在开发一个小型地图应用程序,到目前为止,我有代码可以删除地图图钉并保存它们,以便在重新打开应用程序后它们仍然存在,此类用于主视图控制器:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UISearchBarDelegate, UIPopoverPresentationControllerDelegate {

    var location: CLLocation!
    let locationManager = CLLocationManager()

    @IBOutlet weak var placesMap: MKMapView!
    @IBOutlet weak var addButton: UIBarButtonItem!
    @IBOutlet weak var moreStuff: UIButton!

    // Popover button action
    @IBAction func moreStuff(sender: AnyObject) {
        self.performSegueWithIdentifier("showMoreStuff", sender:self)
        moreStuff.adjustsImageWhenHighlighted = false
    }

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

    // Location function
    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()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
        self.placesMap?.showsUserLocation = true
        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)
            }
        }
    }

我想添加一个按钮,允许一次重置所有引脚(内存)。此按钮位于一个名为“PopoverOptions”的新场景和类上。我有来自应该执行此操作的类的以下代码,但它似乎不起作用,因为一旦用户按下它,地图上就没有任何引脚消失!

@IBOutlet weak var resetMemories: UIButton!

@IBAction func resetMemories(sender: AnyObject) {
    func removeStoredLocations(){
        NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
        NSUserDefaults.standardUserDefaults().synchronize()
    }
}

知道为什么引脚没有被移除吗?我已确保该类以及按钮出口/动作正确链接。

4

1 回答 1

1

您清除用户默认键,但不要更改显示这些引脚的视图控制器上的地图视图。您需要调用 removeAnnotations 从地图中删除注释。

您可以添加一个 viewWillAppear 方法来检测您的注释已被删除并删除它们。像这样的东西:

func viewWillAppear(_ animated: Bool)
{
  if NSUserDefaults.standardUserDefaults().objectForKey("locationArray") == nil 
  {
    if let annotations = self.placesMap.annotations
      self.placesMap?.removeAnnotations(annotations)
  }
}

如果从 userDefaults 中删除了整个注释字典,则编写上面的代码仅从地图中删除注释。这样您就可以避免每次视图控制器重新获得焦点时重新加载注释。

顺便说一句,你说你的新场景叫做“PopoverOptions”。如果场景以弹出框形式呈现,则上述方法可能不起作用。(我认为 viewWillAppear 不会在弹出窗口被解除时在视图控制器上被调用,但我不记得了。)

于 2016-03-26T13:09:21.210 回答