Finding Your Way Around Map Kit

  • Published on
    15-Jan-2015

  • View
    2.806

  • Download
    5

Embed Size (px)

DESCRIPTION

The basics of Core Location and Map Kit.As presented to the MN Cocoa Heads Group for the April 2012 Meeting.Code Available Here:https://github.com/mobiledatasolutions/MDSMapKitDemoLicensed Under Attribution-NonCommercial-NoDerivs 3.0http://creativecommons.org/licenses/by-nc-nd/3.0/us/

Transcript

<ul><li> 1. FINDING YOUR WAY AROUND MAPKITbyRyan</li></ul> <p> 2. Johnson @_RyanJohnson_ MOBILE DATA SOLUTIONSMonday, April 16, 12 3. WHAT WELL COVER Core LocationThe Basics Simulating Location Geocoding Map Kit The Basics Annotations Overlays Advanced Drawing &amp; AnimationMonday, April 16, 12 4. CORE LOCATION THE 5. B ACKENDMonday, April 16, 12 6. GETTING THE USERS LOCATIONCLLocationManagerMonday, April 16, 12 7. GETTING THE USERS LOCATION CLLocationManager1. Instantiate a CLLocationManager object: manager = [[CLLocationManager alloc] init];Monday, April 16, 12 8. GETTING THE USERS LOCATION CLLocationManager1. Instantiate a CLLocationManager object: manager = [[CLLocationManager alloc] init];2. Set a delegate: manager.delegate = (id)self;Monday, April 16, 12 9. GETTING THE USERS LOCATIONCLLocationManager1. Instantiate a CLLocationManager object: manager = [[CLLocationManager alloc] init];2. Set a delegate: manager.delegate = (id)self;3. Set your desired accuracy &amp; start monitoring: manager.desiredAccuracy = kCLLocationAccuracyBest; [manager startUpdatingLocation];Monday, April 16, 12 10. GETTING THE USERS LOCATIONCLLocationManager1. Instantiate a CLLocationManager object: manager = [[CLLocationManager alloc] init];2. Set a delegate: manager.delegate = (id)self;3. Set your desired accuracy &amp; start monitoring: manager.desiredAccuracy = kCLLocationAccuracyBest; [manager startUpdatingLocation];4. Location updates are sent to the delegate method: - (void) locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;Monday, April 16, 12 11. GETTING THE USERS LOCATION CLLocationManager1. Instantiate a CLLocationManager object: manager = [[CLLocationManager alloc] init];2. Set a delegate: manager.delegate = (id)self;3. Set your desired accuracy &amp; start monitoring: manager.desiredAccuracy = kCLLocationAccuracyBest; [manager startUpdatingLocation];4. Location updates are sent to the delegate method: - (void) locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation; PROTiP: 12. GPS 13. hates 14. batteries. 15. You 16. can 17. help 18. save 19. the 20. users 21. battery 22. by 23. setting 24. the 25. desired 26. accuracy 27. to 28. what 29. you 30. actually 31. need. 32. Less 33. accurate 34. = 35. more 36. battery 37. life 38. = 39. better 40. UX. manager.desiredAccuracy = kCLLocationAccuracyThreeKilometersMonday, April 16, 12 41. Geography 42. Lesson 43. #1:The 44. World 45. Is 46. Round. Latitude 47. = 48. the 49. east/west Longitude 50. = 51. the 52. north/south The 53. distance 54. or 55. direction 56. between 57. two 58. points 59. is 60. calculated 61. over 62. the 63. sphere 64. of 65. the 66. earth 67. not 68. a 69. straight 70. line.Monday, April 16, 12 71. THE LOCATION MODEL CLLocation location.coordinate - the CLLocationCoordinate2D struct with the latitude and longitude location.horizontalAccuracy - the accuracy of the coordinate in meters location.speed - the users speed in meters per second location.course - the direction the user is headed in degrees Y U NO WORK!? Invalid 72. Coordinates 73. can 74. cause 75. tough 76. to 77. debug 78. issues. 79. You 80. can 81. validate 82. your 83. coordinates 84. withCLLocationCoordinate2DIsValid(CLLocationCoordinate2D coord)Monday, April 16, 12 85. REGION MONITORING CLRegionEasy to use geofencing - lets you know when a user enters or exits a defined circular geographic region.Can continue to run in the background, relaunching your app when a user enters/exits a region.Shared system resource (limit, identifiers)Create a CLRegion to be monitored: CLLocationCoordinate2D regionCenter = {42.443904, -71.122044}; CLRegion * region = [[CLRegion alloc] initCircularRegionWithCenter:regionCenter radius:1000 identifier:@"A Monitored Region"];Send the CLRegion to a CLLocationManager to be monitored [_locationManager startMonitoringForRegion:region desiredAccuracy:50];Region events are passed to the CLLocationManager delegates methods: - (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region; - (void) locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region;Monday, April 16, 12 86. SIMULATING YOUR LOCATION Can simulate a pin point location or travel a long a series of points If you do nothing, the simulators default location is in San Francisco. Three Ways to Simulate Your Location Default locations in Xcode A GPX File UIAutomation - UIATarget setLocation(coordinateDictionary) PROTIP: 87. Only 88. simulate 89. your 90. location 91. on 92. devices 93. dedicated 94. to 95. development. 96. Your 97. simulated 98. location 99. is 100. reported 101. to 102. all 103. apps. 104. So, 105. Maps 106. or 107. Path 108. will 109. think 110. youre 111. in 112. Pyongyang 113. when 114. youre 115. really 116. in 117. Eagan.Monday, April 16, 12 118. CODE DEMOBASICS 119. A ND 120. LOCATION 121. S IMULATIONMonday, April 16, 12 122. GEOCODINGCLGeocoder Reverse geocoding introduced in iOS 3 with MKReverseGeocoder (now depreciated) iOS 5 now allows for forward and reverse geocoding in the new CLGeocoder class. Forward Geocoding - turns an address into a coordinate Reverse Geocoding - turns a coordinate into an addressMonday, April 16, 12 123. CODE DEMO GEOCODINGMonday, April 16, 12 124. RUNNING IN THE BACKGROUND Location services are a permitted background task Enabled in the Info.plist: UIBackgroundModeslocation Region Monitoring does not require the background task permission. Your app will be relaunched in the background to allow for processing the region event.Monday, April 16, 12 125. MAP KIT THE 126. F RONT 127. E NDMonday, April 16, 12 128. Geography 129. Lesson 130. #2:Maps 131. are 132. flat. 133. this 134. is 135. a 136. problem.There 137. are 138. a 139. lot 140. of 141. ways 142. to 143. make 144. a 145. circle 146. flat. 147. Monday, April 16, 12 148. Geography 149. Lesson 150. #2:Maps 151. are 152. flat. 153. this 154. is 155. a 156. problem.There 157. are 158. a 159. lot 160. of 161. ways 162. to 163. make 164. a 165. circle 166. flat. 167. Monday, April 16, 12 168. Geography 169. Lesson 170. #2:Maps 171. are 172. flat. 173. this 174. is 175. a 176. problem.There 177. are 178. a 179. lot 180. of 181. ways 182. to 183. make 184. a 185. circle 186. flat. 187. MKMapView 188. uses 189. a 190. web 191. mercator 192. projection.Monday, April 16, 12 193. Geography 194. Lesson 195. #2:Maps 196. are 197. flat. 198. this 199. is 200. a 201. problem.There 202. are 203. a 204. lot 205. of 206. ways 207. to 208. make 209. a 210. circle 211. flat. 212. MKMapView 213. uses 214. a 215. web 216. mercator 217. projection.Monday, April 16, 12 218. Geography 219. Lesson 220. #2:Maps 221. are 222. flat. 223. this 224. is 225. a 226. problem.There 227. are 228. a 229. lot 230. of 231. ways 232. to 233. make 234. a 235. circle 236. flat. 237. MKMapView 238. uses 239. a 240. web 241. mercator 242. projection.Monday, April 16, 12 243. Geography 244. Lesson 245. #2:Maps 246. are 247. flat. 248. this 249. is 250. a 251. problem.There 252. are 253. a 254. lot 255. of 256. ways 257. to 258. make 259. a 260. circle 261. flat. 262. MKMapView 263. uses 264. a 265. web 266. mercator 267. projection.Monday, April 16, 12 268. Geography 269. Lesson 270. #2:Maps 271. are 272. flat. 273. this 274. is 275. a 276. problem.There 277. are 278. a 279. lot 280. of 281. ways 282. to 283. make 284. a 285. circle 286. flat. 287. MKMapView 288. uses 289. a 290. web 291. mercator 292. projection.Monday, April 16, 12 293. Geography 294. Lesson 295. #2:Maps 296. are 297. flat. 298. this 299. is 300. a 301. problem.There 302. are 303. a 304. lot 305. of 306. ways 307. to 308. make 309. a 310. circle 311. flat. 312. MKMapView 313. uses 314. a 315. web 316. mercator 317. projection.Mapkit 318. uses 319. its 320. own 321. structures 322. (MKMapPoint, 323. MKMapRect) 324. to 325. handle 326. this 327. conversionMonday, April 16, 12 328. MAPS MKMapView The basic way of showing geographic data Can show the users location with out the need for Core Location mapview.showsUserLocation = YES; [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];Monday, April 16, 12 329. ANNOTATIONSMKAnnotation + MKAnnotationView The basic way of presenting point data on a map Comes with a disclosure pop-out for additional detail MKAnnotation is the basic model (really a protocol) and MKAnnotationView is the view. You add the MKAnnotation to the map, and the map is provided a view by the maps delegate.Monday, April 16, 12 330. OVERLAYSMKOverlay + MKOverlayView Allows you to place paths or custom drawing over a map. Provided basic path types: MKCircle, MKPolyline, MKPolygon Can create your own view classes to easily draw over a map. Custom drawing must be done by overriding: - (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context;Monday, April 16, 12 331. CODE DEMO ANNOTATIONS 332. + 333. O VERLAYSMonday, April 16, 12 334. GDAL Geospatial Data Abstraction Library www.gdal.com or brew install gdal A command line tool to create your own tile images and to convert the projection of images Essential if youre going to put your own images on a mapMonday, April 16, 12 335. CODE DEMO TILED 336. I MAGESMonday, April 16, 12 337. ADDITIONAL RESOURCES WWDC Videos &amp; Sample Code - 2010 Session - Customizing Maps With Overlays KML https://developer.apple.com/library/ios/samplecode/KMLViewer/ Introduction/Intro.html#//apple_ref/doc/uid/DTS40010046 Tiled Images (in the Code Bundle) http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/ getSoftware? wosid=aY5boO0HQiIJ3STghf12IbXXhCt&amp;fileID=26713&amp;code=y&amp;source=x 2011 Session - Visualizing Information Graphically With MapKit Open Source Code - VPPMap (Annotation Clustering)- https://github.com/vicpenap/VPPMap Core Location Utils - https://github.com/100grams/CoreLocationUtilsMonday, April 16, 12 </p>