2016-03-28 9 views
1

私は小さな地図アプリケーションを作っています。私はピン(メモリ)をリセットできるようにしたい、ユーザーが落ちた。ポップオーバーを介してボタンで地図ピンをリセットする

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

enter image description here

私は、マップなどを扱う私のメインビューコントローラクラスのこのコードを持っている:私は、これはポップオーバーを介して表示、「思い出のリセット」ボタンを介して行うことにしたいですここでは、Reset Memoryボタンのアウトレットとアクションがあります。このコードは、PopoverOptionsという名前のポップオーバーの新しいクラスに格納されています。ボタンが押されたときに現在、ピンをマップから削除されていません:

@IBOutlet weak var resetMemories: UIButton! 

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

この作業を取得するためにすべてのヘルプは高く評価され、私はスウィフト

答えて

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

返信いただきありがとうございます:)私はあなたの提案を試みたが、resetMemories()関数が新しいクラスにあるので、placesMapを見ることができません!それ以上の提案はありますか? – Oscar

+0

'resetMemories()'から通知を発し、その通知を受け取ったときに注釈を更新することができます。私は私の答えを編集します。 – jszumski

+0

もう一度ありがとう、私は今これを試してみます。 View ControllerコードをviewDidLoad関数に正しく挿入する必要がありますか? – Oscar

関連する問題