-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapViewController.swift
executable file
·92 lines (64 loc) · 3.07 KB
/
MapViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// MapViewController.swift
// FoodPin
//
// Created by Andrew Hansen on 10/17/14.
// Copyright (c) 2014 Andrew Hansen. All rights reserved.
//
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView:MKMapView!
var restaurant:Restaurant!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
// Convert address to Lat/Long coordinate and annotate it on map
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(restaurant.location, completionHandler: { placemarks, error in // not sure i understand the way this function call and closure work. why doesn't placemarks need to be in [ ]
if error != nil {
println(error)
return
}
if placemarks != nil && placemarks.count > 0 {
let placemark = placemarks[0] as CLPlacemark
// Add Annotation
let annotation = MKPointAnnotation()
annotation.title = self.restaurant.name
annotation.subtitle = self.restaurant.type
annotation.coordinate = placemark.location.coordinate
self.mapView.showAnnotations([annotation], animated: true)
self.mapView.selectAnnotation(annotation, animated: true)
}
})
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let identifier = "MyPin"
// checks to see if annotation is a MKUserLocation (ie. blue dot current location). Even though this feature has not been implemented in the app, we don't want to change its annotation view.
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
// reuse the annotation if possible
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) // trying to understand how the idenifier works
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView.canShowCallout = true
}
let leftIconView = UIImageView(frame: CGRectMake(0, 0, 47, 47))
leftIconView.image = UIImage(named: restaurant.image)
annotationView.leftCalloutAccessoryView = leftIconView
return annotationView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}