Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 轻敲时拆下GMS标记器_Ios_Swift_Google Maps_Google Maps Markers_Uialertcontroller - Fatal编程技术网

Ios 轻敲时拆下GMS标记器

Ios 轻敲时拆下GMS标记器,ios,swift,google-maps,google-maps-markers,uialertcontroller,Ios,Swift,Google Maps,Google Maps Markers,Uialertcontroller,如何使GMS标记器在点击时被删除?我想当一个标记被点击时,会出现一个警报控制器,询问用户是否要保存或删除点击的标记。那么,当按下“移除”按钮时,我将如何移除点击的标记?还有,如果按下“save”(保存),当用户稍后访问地图时,如何保存地图。到目前为止,我有这个基本结构,但不确定如何实现功能: func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { print("didtapmarker")

如何使GMS标记器在点击时被删除?我想当一个标记被点击时,会出现一个警报控制器,询问用户是否要保存或删除点击的标记。那么,当按下“移除”按钮时,我将如何移除点击的标记?还有,如果按下“save”(保存),当用户稍后访问地图时,如何保存地图。到目前为止,我有这个基本结构,但不确定如何实现功能:

  func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {

    print("didtapmarker")
    let alert = UIAlertController(title: "Add this place to wishlist?",
                                  message: "Would you like to add this to your list?",
                                  preferredStyle: .alert)

    let saveAction = UIAlertAction(title: "Save",
                                   style: .default)
    let cancelAction = UIAlertAction(title: "Remove",
                                     style: .default)


    //alert.addAction(defaultAction)
    alert.addAction(saveAction)
    alert.addAction(cancelAction)


    self.present(alert, animated: true, completion: nil)
     return false
}

那么,我将从这里走向何方?如有任何建议,将不胜感激

只需将地图设置为零,标记就会消失

marker.map = nil

要从地图中删除标记,请将
map
设置为
nil

marker.map = nil
在初始化
UIAlertAction
时,可以将上述代码放入
处理程序
闭包中:

let cancelAction = UIAlertAction(title: "Remove",
                                 style: .default) {
    _ in marker.map = nil
}
保存标记要复杂一点。如果一次只保存一个标记,可以使用
UserDefaults

if let latitude = marker.latitude?.doubleValue, let longitude = marker.longitude?.doubleValue {
    UserDefaults.standard.set(latitude, forKey: "lat")
    UserDefaults.standard.set(longitude, forKey: "lon")
}
要在地图上显示保存的标记,请首先检索保存的经度和纬度:

let latitude = UserDefaults.standard.double(forKey: "lat")
let longitude = UserDefaults.standard.double(forKey: "lon")
并使用这些值构造一个新的
GMSMarker


如果要在地图上保存多个标记,则需要使用核心数据。这比
UserDefaults
要复杂一点。我建议你先读一些教程。然后,您可以阅读我做过的类似项目的代码-。

Perfect。看起来我需要深入研究核心数据以及如何使用它,因为我希望用户能够保存多个标记。谢谢你给我指明了正确的方向。