Ios 通过Popover使用按钮重置地图针脚
我正在制作一个小地图应用程序。我想能够重置引脚(内存),用户已放弃。我希望通过一个“重置记忆”按钮完成此操作,该按钮通过Popover显示:Ios 通过Popover使用按钮重置地图针脚,ios,swift,mkmapview,popover,mkpinannotationview,Ios,Swift,Mkmapview,Popover,Mkpinannotationview,我正在制作一个小地图应用程序。我想能够重置引脚(内存),用户已放弃。我希望通过一个“重置记忆”按钮完成此操作,该按钮通过Popover显示: NSNotificationCenter.defaultCenter().addObserverForName("memoriesUpdated", object: nil, queue: nil) { [weak self] (n) -> Void in // remove existing annotations if let a
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)
}
}
}
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)
}
}
}
我的主视图控制器类有以下代码,用于处理地图等:
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)
}
}
}
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)
}
}
}
这是重置记忆按钮的出口和操作,此代码存储在名为“PopoverOptions”的popover的新类中。当前,当按下按钮时,地图上不会移除任何针脚:
@IBOutlet weak var resetMemories: UIButton!
@IBAction func resetMemories(sender: AnyObject) {
func removeStoredLocations(){
NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
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)
}
}
}
感谢您的帮助,我是Swift新手,您正在更新备份阵列,但不是地图视图本身。将
placesMap.removeAnnotations(placesMap.annotations)
添加到resetMemories()
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)
}
}
}
更新:由于操作位于另一个文件中,因此从
resetMemories()
发出通知:
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)
}
}
}
然后在视图控制器中侦听通知并更新PIN:
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)
}
}
}
您正在更新备份阵列,但不更新地图视图本身。将
placesMap.removeAnnotations(placesMap.annotations)
添加到resetMemories()
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)
}
}
}
更新:由于操作位于另一个文件中,因此从
resetMemories()
发出通知:
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)
}
}
}
然后在视图控制器中侦听通知并更新PIN:
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)
}
}
}
感谢您的回复:)我尝试了您的建议,但由于resetMemories()函数位于一个新类中,因此它无法查看placeMap!您有什么进一步的建议吗?您可以从
resetMemories()
发出通知,然后在收到该通知时更新注释。我将编辑我的答案。再次感谢,我现在将尝试这个。视图控制器代码应该插入到viewDidLoad函数中,对吗?是的,您还需要在适当的时候停止观察通知(可能在dealloc
)。好的,谢谢:)我插入了代码,现在在第三行出现错误,指出“可选类型的值”[MKAnnotation]?未打开包装,您是想使用吗?或者?“当我应用建议的修复程序时,它运行时没有错误,但在按下按钮时仍然没有更改地图上的PIN:(感谢您的回答:)我尝试了您的建议,但由于resetmembers()函数位于新类中,它无法看到placemap!您有什么进一步的建议吗?您可以从resetMemories()
发出通知,然后在收到该通知时更新注释。我将编辑我的答案。再次感谢,我现在将尝试这个。视图控制器代码应该插入到viewDidLoad函数中,对吗?是的,您还需要在适当的时候停止观察通知(可能在dealloc
)。好的,谢谢:)我插入了代码,现在在第三行出现错误,指出“可选类型的值”[MKAnnotation]?未打开包装,您是想使用!还是?“当我应用建议的修复程序时,它运行时没有错误,但按下按钮时地图上的针脚仍然没有更改:(