Iphone course 2

33
iPhone Application Development II Janet Huang 2011/11/30

description

 

Transcript of Iphone course 2

Page 1: Iphone course 2

iPhone Application Development II Janet Huang2011/11/30

Page 2: Iphone course 2

Today’s topic

• Model View Control

• Protocol, Delegation, Target/Action

• Location in iPhone

• CoreLocation

• MapKit

• Location-based iPhone Application

Page 3: Iphone course 2

MVC

controller

model view

outlet

target

delegate

data sources

shoulddid

will

count

data

action

Notification & KVO

Page 4: Iphone course 2

IBOutlet & IBAction

controller viewoutlet

target action

#import <UIKit/UIKit.h>

@interface HelloViewController : UIViewController{ IBOutlet UILabel* display;}

- (IBAction)pressButton:(id)sender;@end

ViewControllerInterface Builder

Page 5: Iphone course 2

Delegation pattern

delegate (helper object)delegator

delegate

Page 6: Iphone course 2

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

Page 7: Iphone course 2

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

Page 8: Iphone course 2

@protocol I <NSObject>-(void) f;-(void) g;@end @interface A : NSObject <I> { }@end@implementation A-(void) f { NSLog(@"A: doing f"); }-(void) g { NSLog(@"A: doing g"); }@end @interface B : NSObject <I> { }@end@implementation B-(void) f { NSLog(@"B: doing f"); }-(void) g { NSLog(@"B: doing g"); }@end @interface C : NSObject <I> { id<I> i; // delegation}-(void) toA;-(void) toB;@end @implementation C-(void) f { [i f]; }-(void) g { [i g]; } -(void) toA { [i release]; i = [[A alloc] init]; }-(void) toB { [i release]; i = [[B alloc] init]; }

// constructor-(id)init { if (self = [super init]) { i = [[A alloc] init]; } return self;} // destructor-(void)dealloc { [i release]; [super dealloc]; }@end int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; C *c = [[C alloc] init]; [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 [c release]; [pool drain]; return 0;}

Page 9: Iphone course 2

Delegation• Delegate: a helper object can execute a task for the delegator

• Delegator: delegate a task to the delegate

MyCoreLocationControllerCLLocationManagerDelegatedelegate

the delegator(declare methods)

the delegate(implement methods)

@interface MyCoreLocationController : NSObject <CLLocationManagerDelegate>

protocol

Page 10: Iphone course 2
Page 11: Iphone course 2
Page 12: Iphone course 2
Page 13: Iphone course 2
Page 14: Iphone course 2
Page 15: Iphone course 2

Location in iPhone

• Core Location

• framework for specifying location on the planet

• MapKit

• graphical toolkit for displaying locations on the planet

Page 16: Iphone course 2

CoreLocation• A frameworks to manage location and

heading

• CLLocation (basic object)

• CLLocationManager

• CLHeading

• No UI

• How to get CLLocation?

• use CLLocationManager

Page 17: Iphone course 2

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;

Page 18: Iphone course 2

CoreLocation

• How does it know the location?

• GPS

• Wifi

• Cell network

• The more accurate the technology, the more power it costs

Page 19: Iphone course 2

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.

Page 20: Iphone course 2

CoreLocation• Accuracy-based continuous location monitoring

• Start the monitoring

• Get notified via the CLLocationManager’s delegate

- (void)startUpdatingLocation;- (void)stopUpdatingLocation;

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;

@propertyCLLocationAccuracydesiredAccuracy; @property CLLocationDistance distanceFilter;

Page 21: Iphone course 2

MapKit

• Display a map

• Show user location

• Add annotations on a map

Page 22: Iphone course 2

MKMapView

• 2 ways to create a map

• create with alloc/init

• drag from Library in Interface builder

• MKAnnotation

Page 23: Iphone course 2

MKMapView• Controlling the region the map is displaying

• Can also set the center point only

@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;

@property CLLocationCoordinate2D centerCoordinate;- (void)setCenterCoordinate:(CLLocationCoordinate2D)center animated:(BOOL)animated;

Page 24: Iphone course 2

MKAnnotation

@protocol MKAnnotation <NSObject>@property (readonly) CLLocationCoordinate2D coordinate;@optional@property (readonly) NSString *title;@property (readonly) NSString *subtitle;@end

typedef { CLLocationDegrees latitude; CLLocationDegrees longitude;} CLLocationCoordinate2D;

How to add an annotation on a map? - implement a customized annotation using MKAnnotation protocol

[mapView addAnnotation:myannotation];

- add annotation to MKMapView

Page 25: Iphone course 2

#import <Foundation/Foundation.h>#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject <MKAnnotation>{ CLLocationCoordinate2D coordinate; NSString * title; NSString * subtitle;}@property (nonatomic, assign) CLLocationCoordinate2D coordinate;@property (nonatomic, copy) NSString * title;@property (nonatomic, copy) NSString * subtitle;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coord;

@end

#import "MyAnnotation.h"

@implementation MyAnnotation

@synthesize coordinate;@synthesize title;@synthesize subtitle;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coord { self = [super init]; if (self) { coordinate = coord; } return self;}

- (void) dealloc{ [title release];! [subtitle release]; [super dealloc];}@end

MyAnnotation.h

MyAnnotation.m

Page 26: Iphone course 2

Google Map API

http://maps.googleapis.com/maps/api/geocode/output?parameters

https://maps.googleapis.com/maps/api/geocode/output?parameters

A geolocation api request:

URL parameters:

- address (required)- latlng (required)- sensor (required)- bounds- region- language

http://code.google.com/apis/maps/documentation/geocoding/

Page 27: Iphone course 2

http://maps.googleapis.com/maps/api/geocode/json?address=台北101&sensor=true

{ "results" : [ { "address_components" : [ { "long_name" : "101縣道", "short_name" : "101縣道", "types" : [ "route" ] }, { "long_name" : "New Taipei City", "short_name" : "New Taipei City", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Taiwan", "short_name" : "TW", "types" : [ "country", "political" ] } ], "formatted_address" : "Taiwan, New Taipei City, 101縣道", "geometry" : { "bounds" : { "northeast" : { "lat" : 25.26163510, "lng" : 121.51636480 }, "southwest" : { "lat" : 25.17235040, "lng" : 121.44038660 } }, "location" : { "lat" : 25.20120260, "lng" : 121.4937590 }, "location_type" : "GEOMETRIC_CENTER",

Page 28: Iphone course 2

<?xml version="1.0" encoding="UTF-8"?><GeocodeResponse> <status>OK</status> <result> <type>route</type> <formatted_address>Taiwan, New Taipei City, 101縣道</formatted_address> <address_component> <long_name>101縣道</long_name> <short_name>101縣道</short_name> <type>route</type> </address_component> <address_component> <long_name>New Taipei City</long_name> <short_name>New Taipei City</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>Taiwan</long_name> <short_name>TW</short_name> <type>country</type> <type>political</type> </address_component> <geometry> <location> <lat>25.2012026</lat> <lng>121.4937590</lng> </location> <location_type>GEOMETRIC_CENTER</location_type> <viewport>

http://maps.googleapis.com/maps/api/geocode/xml?address=台北101&sensor=true

Page 29: Iphone course 2

http://maps.google.com/maps/geo?q=台北101

{ "name": "台北101", "Status": { "code": 200, "request": "geocode" }, "Placemark": [ { "id": "p1", "address": "Taiwan, New Taipei City, 101縣道", "AddressDetails": { "Accuracy" : 6, "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "新北市", "Thoroughfare" : { "ThoroughfareName" : "101縣道" }

http://maps.google.com/maps/geo?q=台北101&output=csv

200,6,25.2012026,121.4937590

Page 30: Iphone course 2

Location-based iPhone Implementation

Page 31: Iphone course 2

Hello Location

- get user current location- show a map- show current location- show location information

Page 32: Iphone course 2

Hello Map

- add an annotation on a map- add title and subtitle on this annotation

Page 33: Iphone course 2

Hello Address

- query an address using google geolocation api- show the result on the map