使用UserDefaults保存地图注释

时间:2017-04-14 18:17:50

标签: swift dictionary

我一直在不停地寻找这个解决方案,除了依靠Stackoverflow上的专业知识外别无选择。如果应用关闭并且我一直在使用UserDefault来保存注释,我正在保存地图注释。

这是我发现的Objective C代码,我尝试将其转换为Swift,我认为它存在错误。我不太确定。我将此代码放在viewDidLoad()

这是保存注释

    var pinnedAnnotation: CLLocationCoordinate2D = (parkedCarAnnotation?.coordinate)!
    var coordinateData: NSData = NSData(bytesNoCopy: pinnedAnnotation, length: sizeof(pinnedAnnotation), freeWhenDone: false)
    UserDefaults.standard.set(coordinateData, forKey: pinnedAnnotation)
    UserDefaults.standard.synchronize()

当应用程序打开时,我需要一个加载注释。

我不知道viewDidLoad是否适合放置。以前我把它放在updatesLocation的mapView函数中

已编辑:已添加代码以进一步澄清我所做的需要纠正的内容

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.delegate = self as MKMapViewDelegate
    checkLocationAuthorizationStatus()
    tabBar.delegate = self

    if UserDefaults.standard.object(forKey: "pinnedAnnotation") != nil {
        let annotation = MKPointAnnotation()

        self.mapView.addAnnotation(annotation)
    }

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let pinnedAnnotation: CLLocationCoordinate2D = (parkedCarAnnotation?.coordinate)!
    let locationData = ["latitude": parkedCarAnnotation?.coordinate.latitude, "longitude": parkedCarAnnotation?.coordinate.longitude]
    UserDefaults.standard.set(locationData, forKey: "pinnedAnnotation")
    UserDefaults.standard.synchronize()
    print("Saving data ",  UserDefaults.standard.set(locationData, forKey: "pinnedAnnotation"))

}

 func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    if(item.tag == 0){
        if mapView.annotations.count == 1{
            mapView.addAnnotation(parkedCarAnnotation!)

        } else {
            mapView.removeAnnotation(mapView.annotations as! MKAnnotation)
        }
    } 

extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if let annotation = annotation as? ParkingSpot{
        let identifier = "pin"
        var view: MKPinAnnotationView
        view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        view.canShowCallout = true
        view.animatesDrop = true
        view.pinTintColor = UIColor.orange
        view.calloutOffset = CGPoint(x: -8, y: -3)
        view.rightCalloutAccessoryView = UIButton.init(type:.detailDisclosure) as UIView
        return view
    } else {
        return nil
    }
}

extension ViewController: CLLocationManagerDelegate{
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    centerMapOnLocation(location: CLLocation(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude))
    let locationServiceCoordinate = LocationService.instance.locationManager.location!.coordinate

    parkedCarAnnotation = ParkingSpot(title: "My Parking Spot", locationName: "Find your way back to your car location", coordinate: CLLocationCoordinate2D(latitude: locationServiceCoordinate.latitude, longitude: locationServiceCoordinate.longitude))


}

}

1 个答案:

答案 0 :(得分:0)

我不完全确定你要问的是什么......

对于初学者,当您将数据保存到UserDefaults时,密钥必须是字符串,我也相信您需要将UserDefaults中的数据另存为{{ 1}}

Dictionary

然后检索您要调用的数据

let locationData = ["lat": parkedCarAnnotation?.coordinate?.latitude, "long": parkedCarAnnotation?.coordinate.longitude]  
UserDefaults.standard.set(locationData, forKey: "pinned_annotation")

现在您应该能够使用坐标

设置注释