我有地图应用程序,您可以通过放置引脚来制作注释。如何保存注释,以便在关闭并重新打开应用程序时看到它?
我的注释代码
fprintf(MODEM, "@%s%02X", cBuffer, modem_getChecksum(cBuffer));
fputc(13, MODEM);
如果您想查看整个代码 http://pastebin.com/d89kTrL7
答案 0 :(得分:1)
我会将数据保存为用户默认
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