重置已保存的地图图钉,iOS Swift
Resetting saved map pins, iOS Swift
我正在开发一个小型地图应用程序,到目前为止,我已经有了放置地图图钉并保存它们的代码,以便它们在应用程序重新打开后仍然存在,这个 class 用于主视图控制器:
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)
}
}
}
我想添加一个按钮,允许一次重置所有引脚(内存)。此按钮位于新场景 class 上,名为 "PopoverOptions"。我有来自 class 的以下代码应该执行此操作,但它似乎无法正常工作,因为一旦用户按下它,地图上的图钉就不会消失!
@IBOutlet weak var resetMemories: UIButton!
@IBAction func resetMemories(sender: AnyObject) {
func removeStoredLocations(){
NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
知道为什么没有删除图钉吗?我已确保 class 以及按钮插座/操作正确链接。
您清除了用户默认键,但不更改显示这些图钉的视图控制器上的地图视图。您需要调用 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 在弹出窗口消失时在视图控制器上被调用,但我记不太清了。)
我正在开发一个小型地图应用程序,到目前为止,我已经有了放置地图图钉并保存它们的代码,以便它们在应用程序重新打开后仍然存在,这个 class 用于主视图控制器:
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)
}
}
}
我想添加一个按钮,允许一次重置所有引脚(内存)。此按钮位于新场景 class 上,名为 "PopoverOptions"。我有来自 class 的以下代码应该执行此操作,但它似乎无法正常工作,因为一旦用户按下它,地图上的图钉就不会消失!
@IBOutlet weak var resetMemories: UIButton!
@IBAction func resetMemories(sender: AnyObject) {
func removeStoredLocations(){
NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
知道为什么没有删除图钉吗?我已确保 class 以及按钮插座/操作正确链接。
您清除了用户默认键,但不更改显示这些图钉的视图控制器上的地图视图。您需要调用 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 在弹出窗口消失时在视图控制器上被调用,但我记不太清了。)