Pin an annotation to mapView with the saved coordinates in CoreData

梦想的初衷 提交于 2019-12-23 04:44:02

问题


I have searched now all over the internet and I have tried almost everything. My current project is:

I read the current coordinates from the User Location and save the latitude and longitude in CoreData and the name of this place(user can give a name)

 // MARK: - AddPlace Button Events

@IBAction func onAddButton(sender: AnyObject) {

        //searching the current location
        let locCoord = CLLocationCoordinate2DMake(self.locationManager.location!.coordinate.latitude, self.locationManager.location!.coordinate.longitude)

        // 1. pass the data from the current location to lat and lng
        let lat = locCoord.latitude
        let lng = locCoord.longitude

        let ed = NSEntityDescription.entityForName("Store", inManagedObjectContext: moContext)
        let store = Store(entity: ed!, insertIntoManagedObjectContext: moContext)

        // 2. give the passed data to the array from CoreData
        store.storeName = noteTextField.text
        store.storeLng = lng
        store.storeLat = lat



        do  {
            // 3. save the informations in CoreData
            try moContext.save()
            noteTextField.text  = ""
            lat
            lng

            // 4. testing if the coordinates are saved - it works!
            print(store.storeLat)
            print(store.storeLng)

Now my problem is to display a pin on the saved place in another ViewController with a mapView. I tried:

UPDATE:

storeLat --> stored as Double

storeLng --> stored as Double

storeName --> stored as String

//MARK: - Second ViewController with a mapView



    override func viewDidLoad() {
      super.viewDidLoad()

    // Initialize Fetch Request
    let fetchRequest = NSFetchRequest()

    // Create Entity Description
    let entityDescription = NSEntityDescription.entityForName("Store", inManagedObjectContext: self.moContext)

    // Configure Fetch Request
    fetchRequest.entity = entityDescription
    do {
        stores = try moContext.executeFetchRequest(fetchRequest) as! [Store]
        if stores.count > 0
        {

            print("Test")
            // add the annotation
            let annotation = MKPointAnnotation()
            annotation.coordinate = CLLocationCoordinate2D(latitude: store!.storeLat as! Double, longitude: store!.storeLng as! Double)
            annotation.title = store?.storeName
            self.mapView.addAnnotation(annotation)

        } else
        {
            let alert = SCLAlertView()
            alert.showError("Keine Places", subTitle: "Du hast noch keine Places gespeichert.")
        }
    } catch {
        fatalError("Failed to fetch Places: \(error)")
    }

Now it's going into the if - Statement. But I get a failure when I want to place the annotation. fatal error: unexpectedly found nil while unwrapping an Optional value. I think I have forget something to declare. Can someone help? Thank you already!

UPDATE 2

I found this fantastic site. But I have still some problems.

来源:https://stackoverflow.com/questions/38223475/pin-an-annotation-to-mapview-with-the-saved-coordinates-in-coredata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!