gpt4 book ai didi

ios - 使用按钮 Swift 删除 map 图钉

转载 作者:行者123 更新时间:2023-11-28 12:49:45 25 4
gpt4 key购买 nike

我正在开发一个小型 map 应用程序,我希望有一个按钮可以删除掉落到 map 上的所有图钉(内存)。

到目前为止,我有用于管理位置的代码:

// 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()
}

这里是重置引脚的导出/ Action :

@IBAction func resetMemories(sender: AnyObject) {
func removeStoredLocations(){
NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
NSUserDefaults.standardUserDefaults().synchronize()
placesMap.removeAnnotations(placesMap.annotations)
}
}

当我按下按钮时,当前没有发生错误,但图钉仍保留在 map 上:(

最佳答案

在您的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 是对的。您应该使用他的代码,该代码不会删除用户位置的注释。我正在编辑上面的代码以合并他的代码。

关于ios - 使用按钮 Swift 删除 map 图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36824399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com