Skip to content

Commit 57bd99a

Browse files
committed
update for Swift 1.2, Xcode 6.3 (huge improvement in custom annotation, dragging, animation code)
1 parent 39dc2f4 commit 57bd99a

File tree

8 files changed

+44
-84
lines changed

8 files changed

+44
-84
lines changed

bk2ch21p744maps/ch34p1008maps.xcodeproj/project.pbxproj

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
32BF75331A02A33500D61C2F /* MyAnnotation.m in Sources */ = {isa = PBXBuildFile; fileRef = 32BF75321A02A33500D61C2F /* MyAnnotation.m */; };
10+
323670441AE561AA005B3793 /* MyAnnotation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32D93D5819A7BCA600B017D1 /* MyAnnotation.swift */; };
1111
32D93D5519A7B10C00B017D1 /* MyOverlayRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32D93D5419A7B10C00B017D1 /* MyOverlayRenderer.swift */; };
1212
32D93D5719A7BA2E00B017D1 /* MyOverlay.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32D93D5619A7BA2E00B017D1 /* MyOverlay.swift */; };
13-
32D93D5919A7BCA600B017D1 /* MyAnnotation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32D93D5819A7BCA600B017D1 /* MyAnnotation.swift */; };
1413
32D93D5B19A7BDE400B017D1 /* MyAnnotationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32D93D5A19A7BDE400B017D1 /* MyAnnotationView.swift */; };
1514
32D93D8A19A7CAC900B017D1 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32D93D8919A7CAC900B017D1 /* ViewController.swift */; };
1615
C911DABE182B34D60035F0FD /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C911DABD182B34D60035F0FD /* Foundation.framework */; };
@@ -27,8 +26,6 @@
2726
/* End PBXBuildFile section */
2827

2928
/* Begin PBXFileReference section */
30-
32BF75311A02A33500D61C2F /* MyAnnotation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MyAnnotation.h; sourceTree = "<group>"; };
31-
32BF75321A02A33500D61C2F /* MyAnnotation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MyAnnotation.m; sourceTree = "<group>"; };
3229
32D93D5319A7B10C00B017D1 /* ch34p1008maps-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ch34p1008maps-Bridging-Header.h"; sourceTree = "<group>"; };
3330
32D93D5419A7B10C00B017D1 /* MyOverlayRenderer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MyOverlayRenderer.swift; sourceTree = "<group>"; };
3431
32D93D5619A7BA2E00B017D1 /* MyOverlay.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MyOverlay.swift; sourceTree = "<group>"; };
@@ -107,8 +104,6 @@
107104
C935A901182BEE9B007F97D1 /* clipartdirtbike.gif */,
108105
32D93D5A19A7BDE400B017D1 /* MyAnnotationView.swift */,
109106
32D93D5819A7BCA600B017D1 /* MyAnnotation.swift */,
110-
32BF75311A02A33500D61C2F /* MyAnnotation.h */,
111-
32BF75321A02A33500D61C2F /* MyAnnotation.m */,
112107
32D93D5619A7BA2E00B017D1 /* MyOverlay.swift */,
113108
32D93D5419A7B10C00B017D1 /* MyOverlayRenderer.swift */,
114109
C935A909182BF453007F97D1 /* smileyWithTransparencyTiny.png */,
@@ -197,12 +192,11 @@
197192
isa = PBXSourcesBuildPhase;
198193
buildActionMask = 2147483647;
199194
files = (
195+
323670441AE561AA005B3793 /* MyAnnotation.swift in Sources */,
200196
32D93D5519A7B10C00B017D1 /* MyOverlayRenderer.swift in Sources */,
201197
32D93D5719A7BA2E00B017D1 /* MyOverlay.swift in Sources */,
202-
32BF75331A02A33500D61C2F /* MyAnnotation.m in Sources */,
203198
C911DACE182B34D60035F0FD /* AppDelegate.m in Sources */,
204199
32D93D5B19A7BDE400B017D1 /* MyAnnotationView.swift in Sources */,
205-
32D93D5919A7BCA600B017D1 /* MyAnnotation.swift in Sources */,
206200
32D93D8A19A7CAC900B017D1 /* ViewController.swift in Sources */,
207201
C911DACA182B34D60035F0FD /* main.m in Sources */,
208202
);

bk2ch21p744maps/ch34p1008maps/MyAnnotation.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

bk2ch21p744maps/ch34p1008maps/MyAnnotation.m

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
2-
/*
31
import UIKit
42
import MapKit
53

64
class MyAnnotation : NSObject, MKAnnotation {
7-
8-
var coordinate: CLLocationCoordinate2D
9-
var title: String = ""
10-
var subtitle: String = ""
5+
dynamic var coordinate : CLLocationCoordinate2D
6+
var title: String!
7+
var subtitle: String!
118

129
init(location coord:CLLocationCoordinate2D) {
1310
self.coordinate = coord
1411
super.init()
1512
}
1613

1714
}
18-
*/

bk2ch21p744maps/ch34p1008maps/MyOverlay.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,11 @@ class MyOverlay : NSObject, MKOverlay {
1212
}
1313
}
1414

15-
private let realBoundingMapRect : MKMapRect
16-
var boundingMapRect : MKMapRect {
17-
get {
18-
return realBoundingMapRect
19-
}
20-
}
15+
var boundingMapRect : MKMapRect
2116
var path : UIBezierPath!
2217

2318
init(rect:MKMapRect) {
24-
self.realBoundingMapRect = rect
19+
self.boundingMapRect = rect
2520
super.init()
2621
}
2722

bk2ch21p744maps/ch34p1008maps/ViewController.swift

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ func delay(delay:Double, closure:()->()) {
1313

1414
class ViewController: UIViewController, MKMapViewDelegate {
1515

16-
let which = 10 // 1...10
17-
16+
let which = 1 // 1...10
17+
1818
@IBOutlet var map : MKMapView!
1919
var annloc : CLLocationCoordinate2D!
2020

2121
override func viewDidLoad() {
2222
super.viewDidLoad()
23-
23+
2424
self.map.tintColor = UIColor.greenColor()
2525

2626
let loc = CLLocationCoordinate2DMake(34.927752,-120.217608)
@@ -30,10 +30,10 @@ class ViewController: UIViewController, MKMapViewDelegate {
3030
// let reg = MKCoordinateRegionMakeWithDistance(loc, 1200, 1200)
3131
self.map.region = reg
3232
// or ...
33-
// let pt = MKMapPointForCoordinate(loc)
34-
// let w = MKMapPointsPerMeterAtLatitude(loc.latitude) * 1200
35-
// self.map.visibleMapRect = MKMapRectMake(pt.x - w/2.0, pt.y - w/2.0, w, w)
36-
33+
// let pt = MKMapPointForCoordinate(loc)
34+
// let w = MKMapPointsPerMeterAtLatitude(loc.latitude) * 1200
35+
// self.map.visibleMapRect = MKMapRectMake(pt.x - w/2.0, pt.y - w/2.0, w, w)
36+
3737
self.annloc = CLLocationCoordinate2DMake(34.923964,-120.219558)
3838

3939
if which == 1 {
@@ -50,7 +50,6 @@ class ViewController: UIViewController, MKMapViewDelegate {
5050
ann.title = "Park here"
5151
ann.subtitle = "Fun awaits down the road!"
5252
self.map.addAnnotation(ann)
53-
/*
5453
delay(2) {
5554
UIView.animateWithDuration(0.25) {
5655
var loc = ann.coordinate
@@ -59,7 +58,6 @@ class ViewController: UIViewController, MKMapViewDelegate {
5958
ann.coordinate = loc
6059
}
6160
}
62-
*/
6361
}
6462
if which == 8 {
6563
let lat = self.annloc.latitude
@@ -132,7 +130,7 @@ class ViewController: UIViewController, MKMapViewDelegate {
132130
self.map.addAnnotation(annot)
133131
}
134132
}
135-
133+
136134
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
137135
if which == 3 {
138136
var v : MKAnnotationView! = nil
@@ -141,9 +139,9 @@ class ViewController: UIViewController, MKMapViewDelegate {
141139
v = mapView.dequeueReusableAnnotationViewWithIdentifier(ident)
142140
if v == nil {
143141
v = MKPinAnnotationView(annotation:annotation, reuseIdentifier:ident)
144-
(v as MKPinAnnotationView).pinColor = .Green
142+
(v as! MKPinAnnotationView).pinColor = .Green
145143
v.canShowCallout = true
146-
(v as MKPinAnnotationView).animatesDrop = true
144+
(v as! MKPinAnnotationView).animatesDrop = true
147145

148146
}
149147
v.annotation = annotation
@@ -192,10 +190,10 @@ class ViewController: UIViewController, MKMapViewDelegate {
192190
.imageWithRenderingMode(.AlwaysTemplate)
193191
let iv = UIImageView(image:im)
194192
v.leftCalloutAccessoryView = iv
195-
v.rightCalloutAccessoryView = UIButton.buttonWithType(.InfoLight) as UIButton
193+
v.rightCalloutAccessoryView = UIButton.buttonWithType(.InfoLight) as! UIButton
196194
}
197195
v.annotation = annotation
198-
// v.draggable = true
196+
v.draggable = true
199197
}
200198
return v
201199
}
@@ -204,14 +202,14 @@ class ViewController: UIViewController, MKMapViewDelegate {
204202

205203
func mapView(mapView: MKMapView!, didAddAnnotationViews views: [AnyObject]!) {
206204
if which >= 7 {
207-
for aView in views as [MKAnnotationView] {
205+
for aView in views as! [MKAnnotationView] {
208206
if aView.reuseIdentifier == "bike" {
209207
aView.transform = CGAffineTransformMakeScale(0, 0)
210208
aView.alpha = 0
211209
UIView.animateWithDuration(0.8) {
212210
aView.alpha = 1
213211
aView.transform = CGAffineTransformIdentity
214-
}
212+
}
215213
}
216214
}
217215
}
@@ -253,17 +251,24 @@ class ViewController: UIViewController, MKMapViewDelegate {
253251
let p = MKPlacemark(coordinate:self.annloc, addressDictionary:nil)
254252
let mi = MKMapItem(placemark: p)
255253
mi.name = "A Great Place to Dirt Bike" // label to appear in Maps app
256-
let span = NSValue(MKCoordinateSpan:self.map.region.span)
254+
// setting the span seems to have no effect
255+
//let span = NSValue(MKCoordinateSpan:self.map.region.span)
257256
let opts = [
258257
MKLaunchOptionsMapTypeKey: MKMapType.Hybrid.rawValue,
259-
MKLaunchOptionsMapSpanKey: span
258+
//MKLaunchOptionsMapSpanKey: span
260259
]
261260
mi.openInMapsWithLaunchOptions(opts)
262-
261+
263262
}
264263

265264
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
266-
265+
switch (newState) {
266+
case .Starting:
267+
view.dragState = .Dragging
268+
case .Ending, .Canceling:
269+
view.dragState = .None
270+
default: break
271+
}
267272
}
268-
273+
269274
}

bk2ch21p744maps/ch34p1008maps/ch34p1008maps-Bridging-Header.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
// Use this file to import your target's public headers that you would like to expose to Swift.
33
//
44

5-
#import "MyAnnotation.h"
5+
// #import "MyAnnotation.h"

bk2ch21p761userLocationAndGeocoding/ch34p1024userLocationAndGeocoding/ViewController.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class ViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate {
2424
@IBAction func doButton (sender:AnyObject!) {
2525
let mi = MKMapItem.mapItemForCurrentLocation()
2626
// setting the span doesn't seem to work
27-
//let span = MKCoordinateSpanMake(0.0005, 0.0005)
27+
// let span = MKCoordinateSpanMake(0.0005, 0.0005)
2828
mi.openInMapsWithLaunchOptions([
2929
MKLaunchOptionsMapTypeKey: MKMapType.Standard.rawValue,
30-
//MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan:span)
30+
// MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan:span)
3131
])
3232
}
3333

@@ -52,17 +52,17 @@ class ViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate {
5252
geo.reverseGeocodeLocation(loc) {
5353
(placemarks : [AnyObject]!, error : NSError!) in
5454
if placemarks != nil {
55-
let p = placemarks[0] as CLPlacemark
55+
let p = placemarks[0] as! CLPlacemark
5656
let s = ABCreateStringWithAddressDictionary(p.addressDictionary, false)
5757
println("you are at:\n\(s)") // do something with address
5858
}
5959
}
6060
}
6161

62-
func searchBarSearchButtonClicked(searchBar: UISearchBar!) {
62+
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
6363
searchBar.resignFirstResponder()
6464
let s = searchBar.text
65-
if s == nil || countElements(s) < 5 { return }
65+
if s == nil || count(s) < 5 { return }
6666
let geo = CLGeocoder()
6767
geo.geocodeAddressString(s) {
6868
(placemarks : [AnyObject]!, error : NSError!) in
@@ -71,7 +71,7 @@ class ViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate {
7171
return
7272
}
7373
self.map.showsUserLocation = false
74-
let p = placemarks[0] as CLPlacemark
74+
let p = placemarks[0] as! CLPlacemark
7575
let mp = MKPlacemark(placemark:p)
7676
self.map.removeAnnotations(self.map.annotations)
7777
self.map.addAnnotation(mp)
@@ -90,7 +90,7 @@ class ViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate {
9090
}
9191
let req = MKLocalSearchRequest()
9292
req.naturalLanguageQuery = "Thai restaurant"
93-
req.region = self.map.region
93+
req.region = MKCoordinateRegionMake(loc.coordinate, MKCoordinateSpanMake(1,1))
9494
let search = MKLocalSearch(request:req)
9595
search.startWithCompletionHandler() {
9696
(response : MKLocalSearchResponse!, error : NSError!) in
@@ -99,7 +99,7 @@ class ViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate {
9999
return
100100
}
101101
self.map.showsUserLocation = false
102-
let mi = response.mapItems[0] as MKMapItem // I'm feeling lucky
102+
let mi = response.mapItems[0] as! MKMapItem // I'm feeling lucky
103103
let place = mi.placemark
104104
let loc = place.location.coordinate
105105
let reg = MKCoordinateRegionMakeWithDistance(loc, 1200, 1200)
@@ -130,7 +130,7 @@ class ViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate {
130130
return
131131
}
132132
println("Got restaurant address")
133-
let mi = response.mapItems[0] as MKMapItem // I'm still feeling lucky
133+
let mi = response.mapItems[0] as! MKMapItem // I'm still feeling lucky
134134
let req = MKDirectionsRequest()
135135
req.setSource(MKMapItem.mapItemForCurrentLocation())
136136
req.setDestination(mi)
@@ -142,7 +142,7 @@ class ViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate {
142142
return
143143
}
144144
println("got directions")
145-
let route = response.routes[0] as MKRoute // I'm feeling insanely lucky
145+
let route = response.routes[0] as! MKRoute // I'm feeling insanely lucky
146146
let poly = route.polyline
147147
self.map.addOverlay(poly)
148148
for step in route.steps {

0 commit comments

Comments
 (0)