通过Popover通过按钮重置地图引脚

时间:2016-03-28 14:10:46

标签: ios swift mkmapview popover mkpinannotationview

我正在制作一个小地图应用程序。我希望能够重置引脚(存储器),用户已丢弃。我希望通过“重置记忆”按钮完成此操作,通过Popover显示:

enter image description here

我有我的主视图控制器类的代码,它处理地图等:

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

任何有助于实现这一目标的帮助表示赞赏,我是Swift的新手

1 个答案:

答案 0 :(得分:0)

您正在更新支持数组,但不更新地图视图本身。将placesMap.removeAnnotations(placesMap.annotations)添加到resetMemories()

已更新:由于操作位于其他文件中,因此从resetMemories()

发出通知
NSNotificationCenter.defaultCenter().postNotificationName("memoriesUpdated", object: nil)

然后在您的视图控制器中侦听通知并更新引脚:

NSNotificationCenter.defaultCenter().addObserverForName("memoriesUpdated", object: nil, queue: nil) { [weak self] (n) -> Void in
    // remove existing annotations
    if let annotations = self?.placesMap.annotations {
         self?.placesMap.removeAnnotations(annotations)
    }

    // add new annotations (if any)
    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)
        }
    }
}