我正在开发一个小型 map 应用程序,到目前为止,我有删除 map 图钉并保存它们的代码,以便在重新打开应用程序后它们仍然存在,此类用于主视图 Controller :
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”的新场景和类中。我有来自类的以下代码应该执行此操作,但它似乎无法正常工作,因为一旦用户按下,图钉就不会从 map 上消失!
@IBOutlet weak var resetMemories: UIButton!
@IBAction func resetMemories(sender: AnyObject) {
func removeStoredLocations(){
NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
知道为什么没有移除图钉吗?我已确保该类以及按钮导出/操作正确链接。
最佳答案
您清除了用户默认键,但不要更改显示这些图钉的 View Controller 上的 map View 。您需要调用 removeAnnotations 以从 map 中删除注释。
您可以添加一个 viewWillAppear 方法来检测您的注释是否已被删除并将其删除。像这样:
func viewWillAppear(_ animated: Bool)
{
if NSUserDefaults.standardUserDefaults().objectForKey("locationArray") == nil
{
if let annotations = self.placesMap.annotations
self.placesMap?.removeAnnotations(annotations)
}
}
如果从 userDefaults 中删除了整个注释字典,则编写上面的代码只是为了从 map 中删除注释。这样您就可以避免每次 View Controller 重新获得焦点时都重新加载注释。
顺便说一句,你说你的新场景叫做“PopoverOptions”。如果场景显示在弹出窗口中,则上述内容可能不起作用。 (我不认为当弹出窗口消失时 viewWillAppear 会在 View Controller 上被调用,但我记不太清了。)
关于ios - 重置保存的 map 图钉,iOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36235583/