我正在开发一个小型地图应用程序,到目前为止,我已经有了删除地图图钉并保存它们的代码,以便在重新打开该应用程序后将其保留下来,该类适用于主视图控制器:
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()
}
}
知道为什么不删除销钉吗?我已确保该类以及按钮的输出/操作已正确链接。
最佳答案
您可以清除用户的默认键,但不要在显示这些图钉的视图控制器上更改地图视图。您需要调用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,但我不确定)。
关于ios - 重置保存的 map 图钉,iOS Swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36235583/