An introduction to building mapping applications for the iPhone and iPad Dr Alasdair Allan, Babilim Light Industries Wednesday, 29 September 2010 Frameworks Both Core Location and MapKit Core Location frameworks are available on the iPhone and iPad. The code you’ll see during this presentation Map Kit would work equally well on both platforms. Later on I’m going to go ahead and build a demonstration application for the iPhone. But I could equally well build it for the iPad, without changing very much code. Wednesday, 29 September 2010 Core Location (and the GPS) Wednesday, 29 September 2010 The Core Location Framework is Core Location an abstraction layer in front of several different methods to find the user’s location. It can provide the latitude, longitude and the altitude of the device; along with the level of accuracy to which this is know. • Abstraction layer The actual method used to determine the users location is abstracted away from both • Cell towers (12km falling to 1-3km) the user and the developer. • The only control the developer Skyhook wireless (approx. 100m) has over the chosen method is by requesting a certain level of • accuracy, although the actual Some users may choose to GPS (approx. 40m) accuracy achieved is not explicitly disable reporting of guaranteed. their position. You should therefore always check to see whether location services are enabled before attempting to turn on these services. Wednesday, 29 September 2010 Core Location LocationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; Before the release of the 4.0 SDK the iPhone Simulator will always report if( locationManager.locationServicesEnabled ) { its location as being at Lat. +37.3, Long. -122.0), corresponding to 1 [locationManager startUpdatingLocation]; Infinite Loop, Cupertino, CA. } else { Now it will ask permission to get a location from Mac OS X. However it ... only works with WiFi. If WiFi is turned off, an error will occur. } There is also an annoying bug in the simulator which I’ll talk about later during the live demo. Wednesday, 29 September 2010 Distance filters locationManager.distanceFilter = 1000; // 1km We can filter these location update messages based on a distance filter. Changes in position of less than this amount will not generate an update message to the delegate. Wednesday, 29 September 2010 Desired accuracy locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; We can also set a desired level of accuracy; this will determine the location method(s) used by the Core Location framework to determine the users location. Wednesday, 29 September 2010 Delegate methods The CLLocationManagerDelegate protocol offers two methods, the first is called when a location update. The second is called when an error occurs. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { If the location manager is not able to if( newLocation != oldLocation ) { ascertain the user’s location immediately, ... it reports a kCLErrorLocationUnknown } error and keeps trying. } In most cases you can choose to ignore the error and wait for a new event. - (void)locationManager:(CLLocationManagerH o*w)meavnear gife rt hdei udsFeari dleWniitehs Eyroruorr : (NSError *)error { application access to the location service ... the manager will report a kCLErrorDenied } error. Upon receiving such an error, you should stop the location manager. Wednesday, 29 September 2010 The Magnetometer (The Digital Compass) Wednesday, 29 September 2010 Combining the heading (yaw) information returned by this device with the roll and pitch information returned by the accelerometer will let you determine the true orientation of the iPhone in real-time. But that’s an entirely different webcast. Wednesday, 29 September 2010
Description: