2017-03-22 7 views
0

ヒットリストとToDoアプリの徹底的な検索と構築の後、私はCore DataとMKAnnotationsの実装に近づいていません。私はNSFetchedResults、NSUserDefaultsまたは他のナンセンスの使用に関するあらゆる種類の提案を見てきました。私は、ピンの場所の保存を実装する方法を知りたいし、CoreDataからそれを取得したいと思います。以下は私の無駄な試みです。コアデータを使用したSwift 3でのMKAnnotationsの保存と取り込み

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 

    var locationManager: CLLocationManager! 
    var storedLocations: [StoredLocations]! = [] 

    @IBOutlet weak var mapView: MKMapView! 
    @IBOutlet weak var markSpot: UIBarButtonItem! 
    @IBOutlet weak var segmentedControl: UISegmentedControl! 

    // Part of the Fail 
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     navigationController?.isToolbarHidden = false 

     locationManager = CLLocationManager() 
     locationManager.requestWhenInUseAuthorization() 

     let trackingButton = MKUserTrackingBarButtonItem(mapView: mapView) 
     let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) 
     toolbarItems = [trackingButton, flexibleSpace, markSpot] 

     mapView.delegate = self 
     // More Fail 
     mapView.addAnnotation(getData() as! MKAnnotation) 
    } 

     // Miserable Fail.. Again 
     func getData() { 
      do { 
      storedLocations = try context.fetch(StoredLocations.fetchRequest()) 
      } 
      catch { 
       print("Fetching Failed") 
      } 

     } 

    @IBAction func markSpotPressed(_ sender: Any) { 
     let annotation = MKPointAnnotation() 
     let userLatitude = mapView.userLocation.coordinate.latitude 
     let userLongitude = mapView.userLocation.coordinate.longitude 
     annotation.coordinate = CLLocationCoordinate2D(latitude: userLatitude, longitude: userLongitude) 
     annotation.title = "Dropped Pin" 
     mapView.addAnnotation(annotation) 

    // Another Part of the Fail 
     let newPin = StoredLocations(context: context) 
     newPin.latitude = userLatitude 
     newPin.longitude = userLongitude 
     (UIApplication.shared.delegate as! AppDelegate).saveContext() 

    } 
+0

あなたのコードはあまりにも離れすぎて見えません。 'storedLocations'に返されたオブジェクトをループし、緯度と経度を使ってアノテーションオブジェクトを再作成する必要があります。 – Paulw11

+0

@ Paulw11私の一日を知っていることは、デモを読んでビルドするのは完全な浪費ではありません。私があまり遠く離れていないなら、私はそれで寝るか、誰かが来て、それを助けます。私の脳は揚げられている。 – Chilly

答えて

1

あなたgetData()関数は何も返さない、そしてそれがなかった場合でも、あなたは、複数の格納されたオブジェクトを持っているかもしれないので、それは、MKAnnotationを返却したいとは思わないでしょう。 MKAnnotation

override func viewDidLoad() { 
    super.viewDidLoad() 

    navigationController?.isToolbarHidden = false 

    locationManager = CLLocationManager() 
    locationManager.requestWhenInUseAuthorization() 

    let trackingButton = MKUserTrackingBarButtonItem(mapView: mapView) 
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) 
    toolbarItems = [trackingButton, flexibleSpace, markSpot] 

    mapView.delegate = self 

    if let annotations = getData() { 
     mapView.addAnnotations(annotations) 
    } 
} 

func getData() -> [MKAnnotation]? { 

    do { 
     storedLocations = try context.fetch(StoredLocations.fetchRequest()) 
     var annotations = [MKAnnotation]() 
     for storedLocation in storedLocations { 
      let newAnnotation = MKPointAnnotation() 
      newAnnotation.coordinate.latitude = storedLocation.userLatitude 
      newAnnotation.coordinate.longitude = storedLocation.userLongitude 
      annotations.append(newAnnotation) 
     } 
     return annotations 
    } 
    catch { 
     print("Fetching Failed") 
    } 
    return nil 
} 
+0

それはそれを解決します。 storedLocationsで使用した変数の1つにマイナーな変更があります。助けてくれてありがとう、この質問はSOに対処されて表示されません、質問は未回答座っています。今週末に削除を進めることができます。 – Chilly

関連する問題