Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import UIKit
  2. import MapKit
  3. import Foundation
  4. import CoreData
  5.  
  6. class MapViewController: UIViewController, MKMapViewDelegate {
  7. @IBOutlet weak var mapOutlet: MKMapView!
  8.  
  9. var latitude = 100.0 as Double
  10. var longitude = 120.0 as Double
  11.  
  12. var pins = [NSManagedObject]()
  13.  
  14. func pullPins() {
  15. let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
  16. let managedContext = appDelegate.managedObjectContext
  17.  
  18. let fetchRequest = NSFetchRequest(entityName: "Pin")
  19.  
  20. do {
  21. let results = try managedContext.executeFetchRequest(fetchRequest)
  22. pins = results as! [NSManagedObject]
  23.  
  24. for pin in pins {
  25. let pinLat = pin.valueForKey("latitude") as! Double
  26. let pinLon = pin.valueForKey("longitude") as! Double
  27.  
  28. let pinCoordinate = CLLocationCoordinate2D(latitude: pinLat, longitude: pinLon)
  29.  
  30. let annotation = MKPointAnnotation()
  31. annotation.coordinate = pinCoordinate
  32.  
  33. self.mapOutlet.addAnnotation(annotation)
  34. }
  35. }
  36. catch let error as NSError {
  37. print("Could not fetch (error), (error.userInfo)")
  38. }
  39. }
  40.  
  41.  
  42. func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
  43. print("Pin Tapped")
  44. }
  45.  
  46. override func viewDidLoad() {
  47. super.viewDidLoad()
  48.  
  49. pullPins()
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement