【问题标题】:Showing drop pin after relaunch app?重新启动应用程序后显示放置图钉?
【发布时间】:2015-05-20 17:31:35
【问题描述】:

我有带有地图的应用程序,您可以在其中通过放置图钉进行注释。如何保存注释,以便在应用程序关闭和重新打开时可以看到?

我的注释代码

    func addAnnotation(gesture: UIGestureRecognizer) {

    if gesture.state == UIGestureRecognizerState.Began {

        var touch = gesture.locationInView(self.Mapa)
        var coordinate = Mapa.convertPoint(touch, toCoordinateFromView: self.Mapa)

        var location = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)

        var loc = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)

        CLGeocoder().reverseGeocodeLocation(loc, completionHandler: { (placemarks, error) -> Void in

            if error == nil {
                let placemark = CLPlacemark(placemark: placemarks[0] as! CLPlacemark)

                self.cislo = placemark.subThoroughfare != nil ? placemark.subThoroughfare : ""
                self.adresa = placemark.thoroughfare != nil ? placemark.thoroughfare : ""
                self.mesto = placemark.subAdministrativeArea != nil ? placemark.subAdministrativeArea : ""
                self.krajina = placemark.administrativeArea != nil ? placemark.administrativeArea : ""
                let annotation = MKPointAnnotation()
                annotation.coordinate = placemark.location.coordinate
                annotation.title = self.adresa! + " " + self.cislo!
                self.Mapa.addAnnotation(annotation)
                println("Špendlík pridaný!")


            }

        })

    }

}

如果您想查看整个代码 http://pastebin.com/d89kTrL7

【问题讨论】:

  • 退出后台模式并返回或终止应用程序后返回?
  • 终止应用后返回
  • 您可以使用 NSUserDefaults 保存经度和纬度,然后从 NSUserDefauls 中检索它,然后使用来自 userdefaults 的信息再次显示 pin 注释
  • 你能给我写代码示例吗?因为我从来不用它。
  • 我应该写一个关于你应该做什么的概述吗?那么我现在可以帮忙

标签: ios xcode swift annotations save


【解决方案1】:

我会将数据保存到 userdefaults 中

 func addAnnotation(gesture: UIGestureRecognizer) {
       if gesture.state == UIGestureRecognizerState.Began {
        var touch = gesture.locationInView(self.Mapa)
        var coordinate = Mapa.convertPoint(touch, toCoordinateFromView: self.Mapa)

        let defaults = NSUserDefaults.standardUserDefaults()
        defaults.setDouble(coordinate.longitude, forKey: "longitudeNameKey")
        defaults.setDouble(coordinate.latitude, forKey: "latitudeNameKey")
        defaults.synchronize()

        var location = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
        var loc = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)

        CLGeocoder().reverseGeocodeLocation(loc, completionHandler: { (placemarks, error) -> Void in

            if error == nil {
                let placemark = CLPlacemark(placemark: placemarks[0] as! CLPlacemark)

                self.cislo = placemark.subThoroughfare != nil ? placemark.subThoroughfare : ""
                self.adresa = placemark.thoroughfare != nil ? placemark.thoroughfare : ""
                self.mesto = placemark.subAdministrativeArea != nil ? placemark.subAdministrativeArea : ""
                self.krajina = placemark.administrativeArea != nil ? placemark.administrativeArea : ""
                let annotation = MKPointAnnotation()
                annotation.coordinate = placemark.location.coordinate
                annotation.title = self.adresa! + " " + self.cislo!
                self.Mapa.addAnnotation(annotation)
                println("Špendlík pridaný!")


            }

        })

    }

}

您可以在创建注释时将信息保存到 NSUserDefaults。在 viewDidLoad 方法的某处,您只需从用户默认值中获取所有信息,然后显示注释。

    override func viewDidLoad() {
      super.viewDidLoad()

      loadAnnotationFromUserDefaults()

    }

使用loadAnnotationFromUserDefaults 方法反序列化之前保存到NSUserDefaults 的坐标列表。通过此方法,您还可以将坐标作为注释加载到地图视图上。

 func  loadAnnotationFromUserDefaults(){
        let defaults = NSUserDefaults.standardUserDefaults()
        let  long= defaults.doubleForKey("longitudeNameKey")
        let lat = defaults.doubleForKey("latitudeNameKey")
        println("\(long)")
        println("\(lat)")
         //You got the coordinates that you lost after terminating now load the coordinates as annotation to mapview
    }

您应该设置新坐标并终止应用程序..注意坐标..现在再次重新打开您的应用程序..现在您再次看到日志上的内容

P.S 代码未经测试,应根据您的应用程序架构进行更改...仅供参考。

这是我为您设置的演示项目 https://drive.google.com/open?id=0B6dTvD1JbkgBRnN2QllWWlJqd0E&authuser=0

【讨论】:

  • yeah..theres where you have to put your mind on..每次它添加到数组的新实例时..您必须在同一个数组中附加新注释..可能是从用户默认值中获取数组并添加到那里...请尝试自己做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多