Find selected MapKit Annotation's postID












0















I'm trying to set the selected MapKit's Annotation ID (mapEventID in my case) on didSelect and to use when Swift is preparing the segue:



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let postViewVC = segue.destination as! PostView
postViewVC.eventID = mapEventID
}


mapEventID is initialized in the GeoFire observer:



func retrieveAllEvents () {

let postDB = Database.database().reference().child("DBName")

postDB.observe(.childAdded) { (snapshot) in

let snapshotValue = snapshot.value as! Dictionary <String,AnyObject>
let postTitle = snapshotValue ["EventTitle"]!
let postLocation = snapshotValue ["VisibleLocation"]!
let eventID = snapshotValue ["EventID"]
let postTime = snapshotValue ["EventDateTimeStart"]!

let date = self.convertTimestamp(serverTimestamp: postTime as! Double)
let mapEventDetails : String = "(date) - (postLocation)"

self.geoFire.getLocationForKey(locationID! as! String) { (location, error) in
if (error != nil) {
print("An error occurred getting the location for eventID:", eventID as Any)

} else if (location != nil) {

let postLat = location?.coordinate.latitude
let postLong = location?.coordinate.longitude
let coordinate = CLLocationCoordinate2D(latitude: postLat!, longitude: postLong!)
let annotation = EventAnnotation (coordinate: coordinate, withKey: eventID as! String, title: mapEventDetails, subtitle: mapEventDetails)

self.mapEventID = annotation.eventKey
self.eventMap.addAnnotation(annotation)

} else {
print("GeoFire does not contain a location for:", eventID as Any)
}
}
}
}


The problem is that it always uses the most recently added post's ID as the mapEventID and not the one the user actually tapped on. This makes sense because when the IDs are retrieved from Firebase through the observer it 'observes' for all the keys and the most recent one becomes the ID.



How do I obtain the specific mapEventID when the user taps on the annotation? At the moment I have:



func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
mapEventID = //How do I change the EventID here?
}


I was hoping I could add the ID to the annotation's Description' property in the Geofire observer but Swift does not provide any syntax for that in:



let annotation = EventAnnotation (coordinate: coordinate, withKey: eventID as! String, title: postTitle, subtitle: eventDetails)









share|improve this question



























    0















    I'm trying to set the selected MapKit's Annotation ID (mapEventID in my case) on didSelect and to use when Swift is preparing the segue:



    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let postViewVC = segue.destination as! PostView
    postViewVC.eventID = mapEventID
    }


    mapEventID is initialized in the GeoFire observer:



    func retrieveAllEvents () {

    let postDB = Database.database().reference().child("DBName")

    postDB.observe(.childAdded) { (snapshot) in

    let snapshotValue = snapshot.value as! Dictionary <String,AnyObject>
    let postTitle = snapshotValue ["EventTitle"]!
    let postLocation = snapshotValue ["VisibleLocation"]!
    let eventID = snapshotValue ["EventID"]
    let postTime = snapshotValue ["EventDateTimeStart"]!

    let date = self.convertTimestamp(serverTimestamp: postTime as! Double)
    let mapEventDetails : String = "(date) - (postLocation)"

    self.geoFire.getLocationForKey(locationID! as! String) { (location, error) in
    if (error != nil) {
    print("An error occurred getting the location for eventID:", eventID as Any)

    } else if (location != nil) {

    let postLat = location?.coordinate.latitude
    let postLong = location?.coordinate.longitude
    let coordinate = CLLocationCoordinate2D(latitude: postLat!, longitude: postLong!)
    let annotation = EventAnnotation (coordinate: coordinate, withKey: eventID as! String, title: mapEventDetails, subtitle: mapEventDetails)

    self.mapEventID = annotation.eventKey
    self.eventMap.addAnnotation(annotation)

    } else {
    print("GeoFire does not contain a location for:", eventID as Any)
    }
    }
    }
    }


    The problem is that it always uses the most recently added post's ID as the mapEventID and not the one the user actually tapped on. This makes sense because when the IDs are retrieved from Firebase through the observer it 'observes' for all the keys and the most recent one becomes the ID.



    How do I obtain the specific mapEventID when the user taps on the annotation? At the moment I have:



    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    mapEventID = //How do I change the EventID here?
    }


    I was hoping I could add the ID to the annotation's Description' property in the Geofire observer but Swift does not provide any syntax for that in:



    let annotation = EventAnnotation (coordinate: coordinate, withKey: eventID as! String, title: postTitle, subtitle: eventDetails)









    share|improve this question

























      0












      0








      0








      I'm trying to set the selected MapKit's Annotation ID (mapEventID in my case) on didSelect and to use when Swift is preparing the segue:



      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      let postViewVC = segue.destination as! PostView
      postViewVC.eventID = mapEventID
      }


      mapEventID is initialized in the GeoFire observer:



      func retrieveAllEvents () {

      let postDB = Database.database().reference().child("DBName")

      postDB.observe(.childAdded) { (snapshot) in

      let snapshotValue = snapshot.value as! Dictionary <String,AnyObject>
      let postTitle = snapshotValue ["EventTitle"]!
      let postLocation = snapshotValue ["VisibleLocation"]!
      let eventID = snapshotValue ["EventID"]
      let postTime = snapshotValue ["EventDateTimeStart"]!

      let date = self.convertTimestamp(serverTimestamp: postTime as! Double)
      let mapEventDetails : String = "(date) - (postLocation)"

      self.geoFire.getLocationForKey(locationID! as! String) { (location, error) in
      if (error != nil) {
      print("An error occurred getting the location for eventID:", eventID as Any)

      } else if (location != nil) {

      let postLat = location?.coordinate.latitude
      let postLong = location?.coordinate.longitude
      let coordinate = CLLocationCoordinate2D(latitude: postLat!, longitude: postLong!)
      let annotation = EventAnnotation (coordinate: coordinate, withKey: eventID as! String, title: mapEventDetails, subtitle: mapEventDetails)

      self.mapEventID = annotation.eventKey
      self.eventMap.addAnnotation(annotation)

      } else {
      print("GeoFire does not contain a location for:", eventID as Any)
      }
      }
      }
      }


      The problem is that it always uses the most recently added post's ID as the mapEventID and not the one the user actually tapped on. This makes sense because when the IDs are retrieved from Firebase through the observer it 'observes' for all the keys and the most recent one becomes the ID.



      How do I obtain the specific mapEventID when the user taps on the annotation? At the moment I have:



      func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
      mapEventID = //How do I change the EventID here?
      }


      I was hoping I could add the ID to the annotation's Description' property in the Geofire observer but Swift does not provide any syntax for that in:



      let annotation = EventAnnotation (coordinate: coordinate, withKey: eventID as! String, title: postTitle, subtitle: eventDetails)









      share|improve this question














      I'm trying to set the selected MapKit's Annotation ID (mapEventID in my case) on didSelect and to use when Swift is preparing the segue:



      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      let postViewVC = segue.destination as! PostView
      postViewVC.eventID = mapEventID
      }


      mapEventID is initialized in the GeoFire observer:



      func retrieveAllEvents () {

      let postDB = Database.database().reference().child("DBName")

      postDB.observe(.childAdded) { (snapshot) in

      let snapshotValue = snapshot.value as! Dictionary <String,AnyObject>
      let postTitle = snapshotValue ["EventTitle"]!
      let postLocation = snapshotValue ["VisibleLocation"]!
      let eventID = snapshotValue ["EventID"]
      let postTime = snapshotValue ["EventDateTimeStart"]!

      let date = self.convertTimestamp(serverTimestamp: postTime as! Double)
      let mapEventDetails : String = "(date) - (postLocation)"

      self.geoFire.getLocationForKey(locationID! as! String) { (location, error) in
      if (error != nil) {
      print("An error occurred getting the location for eventID:", eventID as Any)

      } else if (location != nil) {

      let postLat = location?.coordinate.latitude
      let postLong = location?.coordinate.longitude
      let coordinate = CLLocationCoordinate2D(latitude: postLat!, longitude: postLong!)
      let annotation = EventAnnotation (coordinate: coordinate, withKey: eventID as! String, title: mapEventDetails, subtitle: mapEventDetails)

      self.mapEventID = annotation.eventKey
      self.eventMap.addAnnotation(annotation)

      } else {
      print("GeoFire does not contain a location for:", eventID as Any)
      }
      }
      }
      }


      The problem is that it always uses the most recently added post's ID as the mapEventID and not the one the user actually tapped on. This makes sense because when the IDs are retrieved from Firebase through the observer it 'observes' for all the keys and the most recent one becomes the ID.



      How do I obtain the specific mapEventID when the user taps on the annotation? At the moment I have:



      func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
      mapEventID = //How do I change the EventID here?
      }


      I was hoping I could add the ID to the annotation's Description' property in the Geofire observer but Swift does not provide any syntax for that in:



      let annotation = EventAnnotation (coordinate: coordinate, withKey: eventID as! String, title: postTitle, subtitle: eventDetails)






      ios swift firebase mapkit geofire






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 7:19









      BrewskiBrewski

      1811318




      1811318
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Try the following code:



          func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
          if let annotation = view.annotation as? EventAnnotation {
          mapEventID = annotation.eventKey
          }
          }





          share|improve this answer
























          • Perfect, I got confused between Annotation and AnnotationView - I was trying to cast the AnnotationView instead of the Annotation to EventAnnotation.

            – Brewski
            Nov 27 '18 at 5:58












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476321%2ffind-selected-mapkit-annotations-postid%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Try the following code:



          func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
          if let annotation = view.annotation as? EventAnnotation {
          mapEventID = annotation.eventKey
          }
          }





          share|improve this answer
























          • Perfect, I got confused between Annotation and AnnotationView - I was trying to cast the AnnotationView instead of the Annotation to EventAnnotation.

            – Brewski
            Nov 27 '18 at 5:58
















          1














          Try the following code:



          func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
          if let annotation = view.annotation as? EventAnnotation {
          mapEventID = annotation.eventKey
          }
          }





          share|improve this answer
























          • Perfect, I got confused between Annotation and AnnotationView - I was trying to cast the AnnotationView instead of the Annotation to EventAnnotation.

            – Brewski
            Nov 27 '18 at 5:58














          1












          1








          1







          Try the following code:



          func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
          if let annotation = view.annotation as? EventAnnotation {
          mapEventID = annotation.eventKey
          }
          }





          share|improve this answer













          Try the following code:



          func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
          if let annotation = view.annotation as? EventAnnotation {
          mapEventID = annotation.eventKey
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '18 at 1:55









          Kosuke OgawaKosuke Ogawa

          5,52022444




          5,52022444













          • Perfect, I got confused between Annotation and AnnotationView - I was trying to cast the AnnotationView instead of the Annotation to EventAnnotation.

            – Brewski
            Nov 27 '18 at 5:58



















          • Perfect, I got confused between Annotation and AnnotationView - I was trying to cast the AnnotationView instead of the Annotation to EventAnnotation.

            – Brewski
            Nov 27 '18 at 5:58

















          Perfect, I got confused between Annotation and AnnotationView - I was trying to cast the AnnotationView instead of the Annotation to EventAnnotation.

          – Brewski
          Nov 27 '18 at 5:58





          Perfect, I got confused between Annotation and AnnotationView - I was trying to cast the AnnotationView instead of the Annotation to EventAnnotation.

          – Brewski
          Nov 27 '18 at 5:58




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476321%2ffind-selected-mapkit-annotations-postid%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          To store a contact into the json file from server.js file using a class in NodeJS

          Marschland

          Wiesbaden