Sie sind auf Seite 1von 41

An introduction to

building mapping
applications for the
iPhone and iPad
Dr Alasdair Allan, Babilim Light Industries
Wednesday, 29 September 2010
Frameworks
Core Location
Map Kit
Both Core Location and MapKi t
frameworks are available on the
iPhone and iPad. The code youll
see during this presentation
would work equally well on both
platforms.
Later on Im going to go ahead
and build a demonstration
application for the iPhone. But I
could equally well build i t for the
iPad, wi thout changing very much
code.
Wednesday, 29 September 2010
Core Location
(and the GPS)
Wednesday, 29 September 2010
Core Location

Abstraction layer

Cell towers (12km falling to 1-3km)

Skyhook wireless (approx. 100m)

GPS (approx. 40m)


The Core Location Framework is
an abstraction layer in front of
several different methods to
find the users location. It can
provide the lati tude, longi tude
and the al ti tude of the device;
along wi th the level of accuracy
to which this is know.
The actual method used to
determine the users location is
abstracted away from both
the user and the developer.
The only control the developer
has over the chosen method is
by requesting a certain level of
accuracy, al though the actual
accuracy achieved is not
guaranteed.
Some users may choose to
explici tly disable reporting of
their posi tion. You should
therefore al ways 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;
if( locationManager.locationServicesEnabled ) {
[locationManager startUpdatingLocation];
} else {
...
}
Before the release of the 4.0 SDK the
iPhone Simulator will al ways report
i ts location as being at Lat. +37.3,
Long. -122.0), corresponding to 1
Infini te Loop, Cupertino, CA.
Now i t will ask permission to get a
location from Mac OS X. However i t
only works wi th WiFi. If WiFi is
turned off, an error will occur.
There is also an annoying bug in the
simulator which Ill talk about later
during the li ve demo.
Wednesday, 29 September 2010
Distance flters
locationManager.distanceFilter = 1000; // 1km
We can fil ter these
location update messages
based on a distance fil ter.
Changes in posi tion 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
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:
(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if( newLocation != oldLocation ) {
...
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:
(NSError *)error {
...
}
The CLLocationManagerDelegate
protocol offers t wo methods,
the first is called when a
location update.
The second is called when an
error occurs.
If the location manager is not able to
ascertain the users location immediately,
i t reports a kCLErrorLocationUnknown
error and keeps trying.
In most cases you can choose to ignore
the error and wai t for a new event.
However if the user denies your
application access to the location service
the manager will report a kCLErrorDenied
error.
Upon recei ving 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 wi th the roll and pi tch
information returned by the
accelerometer will let you
determine the true orientation
of the iPhone in real-time.
But thats an entirely different
webcast.
Wednesday, 29 September 2010
Combining the heading (yaw)
information returned by this
device wi th the roll and pi tch
information returned by the
accelerometer will let you
determine the true orientation
of the iPhone in real-time.
But thats an entirely different
webcast.
Wednesday, 29 September 2010
Core Location
LocationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
if( locationManager.locationServicesEnabled &&
locationManager.headingAvailable) {
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
} else {
...
}
Its even more important to
check whether heading
information is available than
i t is to check whether
location services are
available, as the availabili ty
of heading information is
currently restricted to iPhone
3GS devices only.
Wednesday, 29 September 2010
Heading flters
locationManager.headingFilter = 5; // 5 degrees
Wednesday, 29 September 2010
Delegate method
- (void)locationManager:(CLLocationManager *) manager
didUpdateHeading:(CLHeading *) newHeading {
// If the accuracy is valid, process the event.
if (newHeading.headingAccuracy > 0) {
CLLocationDirection theHeading = newHeading.magneticHeading;

...
}
}
Wednesday, 29 September 2010
True heading
CLLocationDirection trueHeading = newHeading.trueHeading;
If location updates are also
enabled, the location manager
returns both the true heading
and magnetic heading values.
If location updates are not
enabled, the location manager
returns only the magnetic
heading value.
Wednesday, 29 September 2010
Device orientation
Heading = X degrees
The magnetic and true headings are
correct when the iPhone device is
held like a tradi tional compass, in
portrai t mode, if the user rotates
the device then the heading
readings will still be in this frame of
reference.
Even though the user has not
changed the direction they are
facing the heading values reported
by the device will have changed.
Wednesday, 29 September 2010
Device orientation
Heading = X + 90 degrees
The magnetic and true headings are
correct when the iPhone device is
held like a tradi tional compass, in
portrai t mode, if the user rotates
the device then the heading
readings will still be in this frame of
reference.
Even though the user has not
changed the direction they are
facing the heading values reported
by the device will have changed.
Wednesday, 29 September 2010
Device orientation
Were going to have to
correct for orientation
before reporting headings
back to the user, and we can
use the devices accelerometer
to determine the device
orientation to do so.
float realHeading = heading;
switch (orientation) {
case UIDeviceOrientationPortrait:
break;
case UIDeviceOrientationPortraitUpsideDown:
realHeading = realHeading + 180.0f;
break;
case UIDeviceOrientationLandscapeLeft:
realHeading = realHeading + 90.0f;
break;
case UIDeviceOrientationLandscapeRight:
realHeading = realHeading - 90.0f;
break;
default:
break;
}
while ( realHeading > 360.0f ) {
realHeading = realHeading - 360.0f;
}
Wednesday, 29 September 2010
Calibration panel
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager {
return YES;
}
The magnetometer readings will be
affected by local magnetic fields, so
the CLLocationManager will attempt
to calibrate i ts heading readings by
displaying a Heading Calibration Panel
before i t starts to issue update
messages. However before i t does so i t
will call a delegate method.
If you return YES from this method,
the CLLocationManager will proceed
to display the device calibration panel
on top of the current window.
The calibration panel prompts the
user to move the device in a figure-of-
eight pattern so that Core Location
can distinguish bet ween the Earths
magnetic field and any local magnetic
fields.
The panel will remain visible until
calibration is complete or until you
dismiss i t by calling the
dismissHeadingCalibrationDisplay:
method in the CLLocationManager
class.
Wednesday, 29 September 2010
Frameworks
Core Location
Map Kit
Wednesday, 29 September 2010
Frameworks
Core Location
Map Kit
Wednesday, 29 September 2010
Map Kit
The MapKi t framework
allows you to embed maps
directly into your views, and
provides support for
annotating these maps and
for performing reverse-
geocoding lookups to
determine information for a
gi ven lati tude and longi tude.
Wednesday, 29 September 2010
@interface RootController : UIViewController <CLLocationManagerDelegate> {
MKMapView *mapView;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@end
View Controller
Create a normal view
controller and create an
MKMapView instance
variable, declaring i t as a
property and an IBOutlet.
Wednesday, 29 September 2010
Interface Builder
Drag and drop an MKMapView
from the Library Window into
the View Window, and connect
i t to the instance variable in
Files Owner.
Wednesday, 29 September 2010

Wednesday, 29 September 2010
Delegate methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:
(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if( newLocation != oldLocation ) {
double miles = 2.0;
double scale = ABS(cos(2*M_PI*newLocation.coordinate.latitude/360.0));
MKCoordinateSpan span;
span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/( scale*69.0 );
MKCoordinateRegion region;
region.span = span;
region.center = newLocation.coordinate;
[self.mapView setRegion:region animated:YES];
self.mapView.showsUserLocation = YES;
}
}
The number of miles spanned by a degree of
longi tude range varies based on the current
lati tude.
For example, one degree of longi tude spans a
distance of approx 69 miles at the equator
but shrinks to 0 at the poles. However unlike
longi tudinal distances, which vary based on
the lati tude, one degree of lati tude is al ways
approx 69 miles (ignoring variations due to
the slightly ellipsoidal shape of the Earth).
Length of 1 degree of Longi tude (miles) =
cosine (lati tude) ! 69 (miles)
If we want to map to move
wi th the user we need to use
Core Location to do that.
There are no hooks in MapKi t.
Wednesday, 29 September 2010

Wednesday, 29 September 2010
@interface SimpleAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coord;
@end
MKAnnotation
If you wan to add annotations
to your map, you have to create
a annotation object the
implements the MKAnnotation
protocol.
Oddly there is no defaul t
implementation of this protocol
in MapKi t.
Wednesday, 29 September 2010
MKAnnotation
@implementation SimpleAnnotation
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coord {
if ( self = [super init] ) {
self.coordinate = coord;
}
return self;
}
- (void)dealloc {
[title release];
[subtitle release];
[super dealloc];
}
@end
Wednesday, 29 September 2010
Adding annotations
SimpleAnnotation *annotation = [[SimpleAnnotation alloc] initWithCoordinate:theCoords];
annotation.title = theTitle;
annotation.subtitle = theSubTitle;
MKCoordinateRegion region = { theCoords, {0.2, 0.2} };
[mapView setRegion:region animated:NO];
[mapView addAnnotation: annotation];
[annotation release];
Wednesday, 29 September 2010
Wednesday, 29 September 2010
Frameworks
Core Location
Map Kit
Wednesday, 29 September 2010
Frameworks
Core Location
Map Kit
Wednesday, 29 September 2010
Going Further
Wednesday, 29 September 2010
Location SDKs
Wednesday, 29 September 2010
Location SDKs
Wednesday, 29 September 2010
Further Reading
http://learningiphoneprogramming.com
http://programmingiphonesensors.com
Wednesday, 29 September 2010
Further Reading
Get 35% of of the print book and
40% of of the ebook version by
entering discount code ABF10.
http://learningiphoneprogramming.com
http://programmingiphonesensors.com
Wednesday, 29 September 2010
OReilly Masterclass
Get 40% of by entering discount code ABF10.
http://oreilly.com/go/location-sensors/
Wednesday, 29 September 2010
OReilly Masterclass
"Buy 1 video, get 1 free. Buy 2 videos, get 2
free..." promotion all through September by
entering discount code BVGVF.
http://oreilly.com/go/location-sensors/
Wednesday, 29 September 2010
Live Demo
Wednesday, 29 September 2010

Das könnte Ihnen auch gefallen