2017-04-14 92 views
0

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

这是我发现的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是否合适放置。以前我把它放在updatingLocation

的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)) 


} 

}

回答

0

我不完全知道是什么你问...

因为当你将数据保存到UserDefaults的关键需求是一个字符串首发,我也相信你需要在UserDefaults以节省您的数据作为Dictionary

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

然后取回你会打电话

if let annotationData = UserDefaults.standard.object(forKey: "pinned_annotation") as? Dictionary { 
    guard let lat = annotationData["lat"], let long = annotationData["long"] else { return } 
    let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long) 
} 

现在的数据,你应该希望能够与设置您的注解坐标

+0

我觉得你还挺回答我的问题,但我的另一半问题是,我在哪里放置代码?我正在使用mapView,并有很多可用的功能。我被告知将它放入func locationManager(_ manager:CLLocationManager,didUpdateLocations locations:[CLLocation]){} –

+0

如果您需要获取用户的当前位置,则只需使用它。如果是这种情况,那么是的,每次用户更新他们的位置时,保存该功能中的数据以保存数据。至于设置注释放在哪里你设置你的用户界面 –

+0

我更新了我的代码上面,你能告诉我我做错了什么吗? –