6. class RealPrinter { // the "delegate"
void print() {
System.out.print("something");
}
}
class Printer { // the "delegator"
RealPrinter p = new RealPrinter(); // create the delegate
void print() {
p.print(); // delegation
}
}
public class Main {
// to the outside world it looks like Printer actually prints.
public static void main(String[] args) {
Printer printer = new Printer();
printer.print();
}
}
java simple example
7. interface I {
void f();
void g();
}
class A implements I {
public void f() { System.out.println("A: doing f()"); }
public void g() { System.out.println("A: doing g()"); }
}
class B implements I {
public void f() { System.out.println("B: doing f()"); }
public void g() { System.out.println("B: doing g()"); }
}
class C implements I {
// delegation
I i = new A();
public void f() { i.f(); }
public void g() { i.g(); }
// normal attributes
public void toA() { i = new A(); }
public void toB() { i = new B(); }
}
public class Main {
public static void main(String[] args) {
C c = new C();
c.f(); // output: A: doing f()
c.g(); // output: A: doing g()
c.toB();
c.f(); // output: B: doing f()
c.g(); // output: B: doing g()
}
}
java complex example
8. @protocol I <NSObject>
-(void) f;
-(void) g;
@end
@interface A : NSObject <I> { } // constructor
@end -(id)init {
@implementation A if (self = [super init]) { i = [[A alloc] init]; }
-(void) f { NSLog(@"A: doing f"); } return self;
-(void) g { NSLog(@"A: doing g"); } }
@end
// destructor
@interface B : NSObject <I> { } -(void)dealloc { [i release]; [super dealloc]; }
@end @end
@implementation B
-(void) f { NSLog(@"B: doing f"); } int main (int argc, const char * argv[]) {
-(void) g { NSLog(@"B: doing g"); } NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]
@end init];
C *c = [[C alloc] init];
@interface C : NSObject <I> { [c f]; // output: A: doing f
id<I> i; // delegation [c g]; // output: A: doing g
} [c toB];
-(void) toA; [c f]; // output: B: doing f
-(void) toB; [c g]; // output: B: doing g
@end [c release];
[pool drain];
@implementation C return 0;
-(void) f { [i f]; } }
-(void) g { [i g]; }
-(void) toA { [i release]; i = [[A alloc]
init]; }
-(void) toB { [i release]; i = [[B alloc]
init]; }
9. Delegation
• Delegate: a helper object can execute a task for the delegator
• Delegator: delegate a task to the delegate
delegate
CLLocationManagerDelegate MyCoreLocationController
the delegator the delegate
(declare methods) (implement methods)
@interface MyCoreLocationController : NSObject <CLLocationManagerDelegate>
protocol
10.
11.
12.
13.
14.
15. Location in iPhone
• Core Location
• framework for specifying location on the
planet
• MapKit
• graphical toolkit for displaying locations
on the planet
16. CoreLocation
• A frameworks to manage location and
heading
• CLLocation (basic object)
• CLLocationManager
• CLHeading
• No UI
• How to get CLLocation?
• use CLLocationManager
17. CoreLocation
• Where is the location? (approximately)
@property (readonly) CLLocationCoordinate2D coordinate;
typedef {
CLLocationDegrees latitude;
CLLocationDegrees longitude;
} CLLocationCoordinate2D;
//meters A negative value means “below sea level.”
@property(readonly)CLLocationDistancealtitude;
18. CoreLocation
• How does it know the location?
• GPS
• Wifi
• Cell network
• The more accurate the technology, the
more power it costs
19. CLLocationManager
• General approach
• Check to see if the hardware you are on/user
supports the kind of location updating you want.
• Create a CLLocationManager instance and set a
delegate to receive updates.
• Configure the manager according to what kind
of location updating you want.
• Start the manager monitoring for location
changes.
20. CoreLocation
• Accuracy-based continuous location monitoring
@propertyCLLocationAccuracydesiredAccuracy;
@property CLLocationDistance distanceFilter;
• Start the monitoring
- (void)startUpdatingLocation;
- (void)stopUpdatingLocation;
• Get notified via the CLLocationManager’s
delegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation;
21. MapKit
• Display a map
• Show user location
• Add annotations on a map
22. MKMapView
• 2 ways to create a map
• create with alloc/init
• drag from Library in Interface builder
• MKAnnotation
23. MKMapView
• Controlling the region the map is displaying
@property MKCoordinateRegion region;
typedef struct {
CLLocationCoordinate2D center;
MKCoordinateSpan span;
} MKCoordinateRegion;
typedef struct {
CLLocationDegrees latitudeDelta;
CLLocationDegrees longitudeDelta;
}
// animated version
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
• Can also set the center point only
@property CLLocationCoordinate2D centerCoordinate;
- (void)setCenterCoordinate:(CLLocationCoordinate2D)center animated:(BOOL)animated;
24. MKAnnotation
How to add an annotation on a map?
- implement a customized annotation using MKAnnotation protocol
@protocol MKAnnotation <NSObject>
@property (readonly) CLLocationCoordinate2D coordinate;
@optional
@property (readonly) NSString *title;
@property (readonly) NSString *subtitle;
@end
typedef {
CLLocationDegrees latitude;
CLLocationDegrees longitude;
} CLLocationCoordinate2D;
- add annotation to MKMapView
[mapView addAnnotation:myannotation];
26. Google Map API
A geolocation api request:
http://maps.googleapis.com/maps/api/geocode/output?parameters
https://maps.googleapis.com/maps/api/geocode/output?parameters
URL parameters:
- address (required)
- latlng (required)
- sensor (required)
- bounds
- region
- language
http://code.google.com/apis/maps/documentation/geocoding/