我正在开发一个小型地图应用程序,我希望有一个按钮,可以删除掉到地图上的所有引脚(存储器)。
到目前为止,我有这个代码来管理位置:
// 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()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Error code: " + error.localizedDescription)
}
这是放下别针的按钮:
@IBOutlet weak var addButton: UIBarButtonItem!
@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()
}
这是重置引脚的插座/动作:
@IBAction func resetMemories(sender: AnyObject) {
func removeStoredLocations(){
NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
NSUserDefaults.standardUserDefaults().synchronize()
placesMap.removeAnnotations(placesMap.annotations)
}
}
当我按下按钮时,当前没有错误,但引脚仍然保留在地图上:(
答案 0 :(得分:2)
在resetMemories
IBAction
中,您创建了一个嵌套函数removeStoredLocations
。然后你永远不会调用那个嵌套函数,所以代码永远不会被执行。我建议摆脱嵌套函数:
@IBAction func resetMemories(sender: AnyObject)
{
NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
NSUserDefaults.standardUserDefaults().synchronize()
let annotationsToRemove = placesMap.annotations.filter
{
$0 !== placesMap.userLocation
}
placesMap.removeAnnotations( annotationsToRemove )
}
或者,如果你真的想要一个嵌套函数,你必须调用它:
@IBAction func resetMemories(sender: AnyObject)
{
//Define our local function removeStoredLocations().
func removeStoredLocations()
{
NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
NSUserDefaults.standardUserDefaults().synchronize()
let annotationsToRemove = placesMap.annotations.filter
{
$0 !== placesMap.userLocation
}
placesMap.removeAnnotations( annotationsToRemove )
}
//Now that we've defined our local function, call it.
removeStoredLocations()
}
Jthomps是对的。您应该使用不删除用户位置注释的代码。我正在编辑上面的代码以合并他的。
答案 1 :(得分:0)
我删除了你在插座中的嵌套功能。这就是为什么它从未被调用过的原因。
尝试更改为此代码段:
@IBAction func resetMemories(sender: AnyObject) {
NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
NSUserDefaults.standardUserDefaults().synchronize()
let annotationsToRemove = placesMap.annotations.filter { $0 !== placesMap.userLocation }
placesMap.removeAnnotations(annotationsToRemove)
}