SlideShare une entreprise Scribd logo
1  sur  37
Télécharger pour lire hors ligne
MapKit 和⽂文本输⼊入




范圣刚,princetoad@gmail.com,www.tfan.org
• 在这⼀一章,我们将使⽤用 MapKit 框架,UITextField
 类以及更多的 delegation 来完成 Whereami 应⽤用。

• ⺫⽬目前,whereami 找到位置并且在控制台打印出
 来。这章结束时,应⽤用程序将显⽰示出当前位置的
 地图.并且⽤用户还有⼀一个可选项,可以使⽤用MapKit
 annotation 标记和命名当前的位置。默认的
 MapKit annotation 显⽰示为地图上的⼀一个红⾊色的⼤大
 头针。
完成后的 Whereami
对象⽰示意图(Object Diagrams)
view objects
• MKAnnotationView
 • 在 MKMapView 上作为图标显⽰示的⼏几个
   MKAnnotationView 的实例

• MKMapView
 • 显⽰示地图,和所记录的位置的标签
• UIActivityIndicatorView
 • 表⽰示当前设备还在⼯工作,没有⽌止步不前
• UITextField
 • 允许⽤用户输⼊入⽂文本来给地图上的当前位置打标签
model objects
• CLLocationManager
 • 和设备交互以确定⽤用户位置
• BNRMapPoint
 • 我们⾃自⼰己创建的模型对象
controller object
• WhereamiViewController
 • 负责处理来⾃自其他对象的更新和请求
 • 是 MKMapView 的 delegate
   • 当⼀一个 view 或者多个 views 被添加上以后 MKMapView 会发
    送 mapView:didAddAnnotationViews:

 • 是 UITextField 的 delegate
   • ⽤用户完成输⼊入⽂文本后,UITextField 会发送
    textFieldShouldReturn:

 • 是 CLLocationManager 的 delegate
   • 发送 locationManager:didUpdateLocations: 通知
    WhereamiViewController 位置已经更新
MapKit 框架
• Core Location framework 告诉我们我们在世界上
 的什么地⽅方
• MapKit framework 把世界显⽰示给我们
• MapKit 的主要⼯工作都是由 MKMapView 类完成的
 • 显⽰示⼀一个地图
 • 追踪触控
 • 以及显⽰示注释(annotation)
把 MapKit framework 加⼊入项⺫⽬目
导⼊入 MapKit 头⽂文件


#import <UIKit/UIKit.h>
// 导⼊入 Core Location framework 的头⽂文件
#import <CoreLocation/CoreLocation.h>
// 导⼊入MapKit framework 的头⽂文件
#import <MapKit/MapKit.h>
声明必要的实例变量

@interface WhereamiViewController : UIViewController
<CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;

   IBOutlet MKMapView *worldView;
   IBOutlet UIActivityIndicatorView *activityIndicator;
   IBOutlet UITextField *locationTitleField;
}
@end
界⾯面属性(Interface Properties)
1.从界⾯面右侧的 object library 中拖拽 MKMapView 到
  UIView 上
2.再拖拽 UITextField, UIActivityIndicatorView 到
  MKMapView 上,适当调整位置和⼤大⼩小
3.设置 ViewController 中实例变量到新增加的视图的
  链接(IBOutlet指⽰示的)
4.设置新增加的视图的 delegate 到 ViewController
XIB 布局
Finished connections
调整 UITextField 属性
• 设置 Placeholder
• 设置 Return Key
设置 UIActivityIndicator 属性
• 不转的时候⾃自动隐藏
• 勾选上“Hides When
 Stopped”
作为 MapView 的 Delegate
• 我们希望 Whereami 启动时,找到当前的位置并在
 地图上显⽰示
• 上⼀一个主题我们直接使⽤用 Core Location 查找⽤用户
 的位置
• MKMapView 实例本⾝身知道如何使⽤用 Core Location
 查找⽤用户位置
 • 设置 MKMapView 的 showUserLocation 属性为 YES,
  然后它就会查找⽤用户的位置并把它显⽰示在地图上
 • 界⾯面加载完以后,WhereamiViewController 会被发送
  viewDidLoad 消息,我们在这⾥里⾯面告诉 MKMapView 来
  更新它的位置。
修改 WhereamiViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
!   // Do any additional setup after loading the view, typically from a nib.
    [worldView setShowsUserLocation:YES];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        locationManager = [[CLLocationManager alloc] init];

        [locationManager setDelegate:self];

        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

        [locationManager startUpdatingLocation];
    }

    return self;
}
MKMapViewDelegate
• delegate 中是否有⼀一个⽤用户位置更新的事件?
• 可以使⽤用⽂文档
⽂文档
• API Reference
• System Guides
• Tools Guides
• Sample Code
mapView:didUpdateUserLocation
• 通知⽤用户位置更新
- (void)mapView:(MKMapView *)mapView
    didUpdateUserLocation:(MKUserLocation *)userLocation
{
}
当前位置居中显⽰示并适当缩放
• 已有数据:MKUserLocation
• 需要调⽤用⽅方法:- (void)setRegion:
 (MKCoordinateRegion)region animated:
 (BOOL)animated
MKCoordinateRegion
    typedef struct {
    ! CLLocationCoordinate2D center;
    ! MKCoordinateSpan span;
    } MKCoordinateRegion;


    typedef struct {
    ! CLLocationDegrees latitude;
    ! CLLocationDegrees longitude;
    } CLLocationCoordinate2D;

    typedef struct {
        CLLocationDegrees latitudeDelta;
        CLLocationDegrees longitudeDelta;
    } MKCoordinateSpan;

    typedef double CLLocationDegrees;
MKUserLocation
@interface MKUserLocation : NSObject <MKAnnotation> {
  @private
    MKUserLocationInternal *_internal;
}

// Returns YES if the user's location is being updated.
@property (readonly, nonatomic, getter=isUpdating) BOOL updating;

// Returns nil if the owning MKMapView's showsUserLocation is NO or the user's location has
yet to be determined.
@property (readonly, retain, nonatomic) CLLocation *location;

// Returns nil if not in MKUserTrackingModeFollowWithHeading
@property (readonly, nonatomic, retain) CLHeading *heading NS_AVAILABLE(NA, 5_0);

// The title to be displayed for the user location annotation.
@property (nonatomic, copy) NSString *title;

// The subtitle to be displayed for the user location annotation.
@property (nonatomic, copy) NSString *subtitle;

@end
MKAnnotation
@protocol MKAnnotation <NSObject>

// Center latitude and longitude of the annotion view.
// The implementation of this property must be KVO compliant.
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@optional

// Title and subtitle for use by selection UI.
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;

// Called as a result of dragging an annotation view.
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate NS_AVAILABLE(NA, 4_0);

@end
MKAnnotation
• MKAnnotation 不是 delegate protocol
• 声明了⼀一套对于任何想要把⾃自⼰己的实例显⽰示到
 map view 上的类⾮非常有⽤用的⽅方法
• ⽐比⽅方附近的酒店,⼯工⼚厂,⽕火⻋车站.... 这些类在应⽤用
 程序中都是⾮非常不同并且层次⽆无关的,但是只要
 他们满⾜足 MKAnnotation, 它们都可以被添加到
 MKMapView
MKAnnotationView
• 当⼀一个满⾜足 MKAnnotation 的对象被添加到
 MKMapView 时,⼀一个MKAnnotationView 的实例
 会被创建并添加到 map view
• 这个 MKAnnotationView 持有⼀一个到这个对象的指
 针,以便需要的时候可以向它请求数据
MKMapView 和 annotation
BNRMapPoint
• 新建⼀一个类 BNRMapPoint, 让它遵守
 MKAnnotation
• 声明两个属性和⼀一个初始化器
BNRMapPoint.h


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

 @interface BNRMapPoint : NSObject <MKAnnotation>
 {

 }

 -(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t;

 @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

 @property (nonatomic, copy) NSString *title;

 @end
BNRMapPoint.m

@implementation BNRMapPoint

@synthesize coordinate, title;

- (id)init
{
    return [self initWithCoordinate:CLLocationCoordinate2DMake(39.91503, 116.4631)
title:@"央视新台址"];
}

- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t
{
    self = [super init];
    if (self) {
        coordinate = c;
        [self setTitle:t];
    }

    return self;
}
@end
给位置打标签
• 现在我们有了⼀一个符合 MKAnnotation 的类
 (BNRMapPoint),我们可以把它的实例显⽰示在
 地图上
• ⽤用户可以在 UITextField 中输⼊入位置的名称,然后
 按下 keyboard 上的 Done 按钮
• 轻按 Done 是增加⼀一个 annotation 的信号,怎么
 才能知道这个事件发⽣生了呢? 还是 Delegation
textFieldShouldReturn
@interface WhereamiViewController : UIViewController
<CLLocationManagerDelegate, MKMapViewDelegate,
UITextFieldDelegate>


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self findLocation];

    [textField resignFirstResponder];

    return YES;
}
first responder
• UIResponder 是 UIKit framework 的⼀一个类,⼀一个
 responder 负责接收和处理与其相关的事件
• UIView 是 UIResponder 的⼦子类,因此 UIView 对象
 可以接收事件
• 按钮是可以处理触控事件的 responder,像 tap
• 晃动设备及点击键盘上的⼀一个键也会产⽣生事件
• responders 之⼀一就是 first responder, 同⼀一时间只能
 有⼀一个 responder 可以作为 first responder
• first responder 处理那些不和屏幕上某⼀一具体位置
 相关的事件。举例:tap 和 shaking的区别
resignFirstResponder
• UITextField 也是⼀一个 responder:它是 UIControl
 的直接⼦子类,UIControl 是 UIView 的⼦子类,UIView
 是 UIResponder 的⼦子类
• 当UITextField 被点击的时候,它通过变成 first
 responder 来处理事件
• 当 UITextField 变成 first responder, ⼀一个键盘就会
 ⾃自动出现在屏幕上;要从屏幕上移除键盘,要通
 过给它发送 resignFirstResponder 消息来告诉
 UITextField 放弃它的 first responder 状态,⼀一旦
 first responder 不再是 UITextField,键盘就会消失
拼合使⽤用
<> 和 “”
• 和 C++ ⼀一样,从框架中包含的⽂文件使⽤用<>尖括
 号,⾃自⼰己编写的头⽂文件使⽤用“”引号
• #import <MapKit/MapKit.h>,表⽰示仅在系统库下
 ⾯面查找这个⽂文件
• #import "BNRMapPoint.h",表⽰示⾸首先在项⺫⽬目⽂文件
 下查找,找不到的话再在系统库下⾯面查找

Contenu connexe

Tendances

深入淺出 Web 容器 - Tomcat 原始碼分析
深入淺出 Web 容器  - Tomcat 原始碼分析深入淺出 Web 容器  - Tomcat 原始碼分析
深入淺出 Web 容器 - Tomcat 原始碼分析Justin Lin
 
iOS程序设计-数据持久化
iOS程序设计-数据持久化iOS程序设计-数据持久化
iOS程序设计-数据持久化qiyutan
 
面向对象的Js培训
面向对象的Js培训面向对象的Js培训
面向对象的Js培训yiditushe
 
07 View Controllers
07 View Controllers07 View Controllers
07 View ControllersTom Fan
 
Javascript share
Javascript shareJavascript share
Javascript shareXu Mac
 
Underscore
UnderscoreUnderscore
Underscorecazhfe
 
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7Justin Lin
 
從 Singleton 談 constructor
從 Singleton 談 constructor從 Singleton 談 constructor
從 Singleton 談 constructorLuba Tang
 
千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7Justin Lin
 
Kissy editor开发与设计
Kissy editor开发与设计Kissy editor开发与设计
Kissy editor开发与设计yiming he
 
页游开发中的 Python 组件与模式
页游开发中的 Python 组件与模式页游开发中的 Python 组件与模式
页游开发中的 Python 组件与模式勇浩 赖
 
论 Python 与设计模式。
论 Python 与设计模式。论 Python 与设计模式。
论 Python 与设计模式。勇浩 赖
 
合久必分,分久必合
合久必分,分久必合合久必分,分久必合
合久必分,分久必合Qiangning Hong
 
Java华为面试题
Java华为面试题Java华为面试题
Java华为面试题yiditushe
 
Javascript之昨是今非
Javascript之昨是今非Javascript之昨是今非
Javascript之昨是今非Tony Deng
 
Java script closures
Java script closuresJava script closures
Java script closuresskywalker1114
 
潜力无限的编程语言Javascript
潜力无限的编程语言Javascript潜力无限的编程语言Javascript
潜力无限的编程语言Javascriptjay li
 
Programming in Objective-C
Programming in Objective-CProgramming in Objective-C
Programming in Objective-CRyan Chung
 
JavaScript Advanced Skill
JavaScript Advanced SkillJavaScript Advanced Skill
JavaScript Advanced Skillfirestoke
 

Tendances (20)

深入淺出 Web 容器 - Tomcat 原始碼分析
深入淺出 Web 容器  - Tomcat 原始碼分析深入淺出 Web 容器  - Tomcat 原始碼分析
深入淺出 Web 容器 - Tomcat 原始碼分析
 
iOS程序设计-数据持久化
iOS程序设计-数据持久化iOS程序设计-数据持久化
iOS程序设计-数据持久化
 
面向对象的Js培训
面向对象的Js培训面向对象的Js培训
面向对象的Js培训
 
07 View Controllers
07 View Controllers07 View Controllers
07 View Controllers
 
Javascript share
Javascript shareJavascript share
Javascript share
 
Underscore
UnderscoreUnderscore
Underscore
 
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
 
從 Singleton 談 constructor
從 Singleton 談 constructor從 Singleton 談 constructor
從 Singleton 談 constructor
 
千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7
 
Kissy editor开发与设计
Kissy editor开发与设计Kissy editor开发与设计
Kissy editor开发与设计
 
页游开发中的 Python 组件与模式
页游开发中的 Python 组件与模式页游开发中的 Python 组件与模式
页游开发中的 Python 组件与模式
 
论 Python 与设计模式。
论 Python 与设计模式。论 Python 与设计模式。
论 Python 与设计模式。
 
合久必分,分久必合
合久必分,分久必合合久必分,分久必合
合久必分,分久必合
 
Java华为面试题
Java华为面试题Java华为面试题
Java华为面试题
 
Node way
Node wayNode way
Node way
 
Javascript之昨是今非
Javascript之昨是今非Javascript之昨是今非
Javascript之昨是今非
 
Java script closures
Java script closuresJava script closures
Java script closures
 
潜力无限的编程语言Javascript
潜力无限的编程语言Javascript潜力无限的编程语言Javascript
潜力无限的编程语言Javascript
 
Programming in Objective-C
Programming in Objective-CProgramming in Objective-C
Programming in Objective-C
 
JavaScript Advanced Skill
JavaScript Advanced SkillJavaScript Advanced Skill
JavaScript Advanced Skill
 

Similaire à 05 MapKit and Text Input

iOS Map and Location
iOS Map and LocationiOS Map and Location
iOS Map and LocationRyan Chung
 
04 Delegation and Core Location
04 Delegation and Core Location04 Delegation and Core Location
04 Delegation and Core LocationTom Fan
 
18 NSUserDefaults
18 NSUserDefaults18 NSUserDefaults
18 NSUserDefaultsTom Fan
 
15 Subclassing UITableViewCell
15 Subclassing UITableViewCell15 Subclassing UITableViewCell
15 Subclassing UITableViewCellTom Fan
 
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAPiOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAPMing-Sian Lin
 
in in der 響應式編程
in in der 響應式編程in in der 響應式編程
in in der 響應式編程景隆 張
 
Backbone js and requirejs
Backbone js and requirejsBackbone js and requirejs
Backbone js and requirejsChi-wen Sun
 
Java script closures
Java script closuresJava script closures
Java script closuresskywalker1114
 
CKAN 技術介紹 (開發篇)
CKAN 技術介紹 (開發篇)CKAN 技術介紹 (開發篇)
CKAN 技術介紹 (開發篇)Chengjen Lee
 
实时任务调度
实时任务调度实时任务调度
实时任务调度Tony Deng
 
Introduction to CodeIgniter
Introduction to CodeIgniterIntroduction to CodeIgniter
Introduction to CodeIgniterChun-Kai Wang
 
再接再勵學 Swift 程式設計
再接再勵學 Swift 程式設計再接再勵學 Swift 程式設計
再接再勵學 Swift 程式設計政斌 楊
 
淺談C#物件導向與DesignPattern.pdf
淺談C#物件導向與DesignPattern.pdf淺談C#物件導向與DesignPattern.pdf
淺談C#物件導向與DesignPattern.pdfBrian Chou 周家禾
 
Dive into kissy
Dive into kissyDive into kissy
Dive into kissyjay li
 

Similaire à 05 MapKit and Text Input (20)

iOS Map and Location
iOS Map and LocationiOS Map and Location
iOS Map and Location
 
04 Delegation and Core Location
04 Delegation and Core Location04 Delegation and Core Location
04 Delegation and Core Location
 
I os 14
I os 14I os 14
I os 14
 
18 NSUserDefaults
18 NSUserDefaults18 NSUserDefaults
18 NSUserDefaults
 
15 Subclassing UITableViewCell
15 Subclassing UITableViewCell15 Subclassing UITableViewCell
15 Subclassing UITableViewCell
 
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAPiOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
 
Chromium多线程滚动
Chromium多线程滚动Chromium多线程滚动
Chromium多线程滚动
 
I os 07
I os 07I os 07
I os 07
 
in in der 響應式編程
in in der 響應式編程in in der 響應式編程
in in der 響應式編程
 
I os 02
I os 02I os 02
I os 02
 
I os 09
I os 09I os 09
I os 09
 
Note something
Note somethingNote something
Note something
 
Backbone js and requirejs
Backbone js and requirejsBackbone js and requirejs
Backbone js and requirejs
 
Java script closures
Java script closuresJava script closures
Java script closures
 
CKAN 技術介紹 (開發篇)
CKAN 技術介紹 (開發篇)CKAN 技術介紹 (開發篇)
CKAN 技術介紹 (開發篇)
 
实时任务调度
实时任务调度实时任务调度
实时任务调度
 
Introduction to CodeIgniter
Introduction to CodeIgniterIntroduction to CodeIgniter
Introduction to CodeIgniter
 
再接再勵學 Swift 程式設計
再接再勵學 Swift 程式設計再接再勵學 Swift 程式設計
再接再勵學 Swift 程式設計
 
淺談C#物件導向與DesignPattern.pdf
淺談C#物件導向與DesignPattern.pdf淺談C#物件導向與DesignPattern.pdf
淺談C#物件導向與DesignPattern.pdf
 
Dive into kissy
Dive into kissyDive into kissy
Dive into kissy
 

Plus de Tom Fan

PhoneGap 通信原理和插件系统
PhoneGap 通信原理和插件系统PhoneGap 通信原理和插件系统
PhoneGap 通信原理和插件系统Tom Fan
 
HTML5 Web workers
HTML5 Web workersHTML5 Web workers
HTML5 Web workersTom Fan
 
Web sockets
Web socketsWeb sockets
Web socketsTom Fan
 
Semantics
SemanticsSemantics
SemanticsTom Fan
 
Multimedia
MultimediaMultimedia
MultimediaTom Fan
 
Intro to-html5
Intro to-html5Intro to-html5
Intro to-html5Tom Fan
 
Html5 history
Html5 historyHtml5 history
Html5 historyTom Fan
 
Geolocation
GeolocationGeolocation
GeolocationTom Fan
 
File api
File apiFile api
File apiTom Fan
 
Deviceaccess
DeviceaccessDeviceaccess
DeviceaccessTom Fan
 
Webstorage
WebstorageWebstorage
WebstorageTom Fan
 
Html5 最重要的部分
Html5 最重要的部分Html5 最重要的部分
Html5 最重要的部分Tom Fan
 
AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状Tom Fan
 
PhoneGap 2.0 开发
PhoneGap 2.0 开发PhoneGap 2.0 开发
PhoneGap 2.0 开发Tom Fan
 
Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发Tom Fan
 
HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型Tom Fan
 
13 UIPopoverController and Modal View Controller
13 UIPopoverController and Modal View Controller13 UIPopoverController and Modal View Controller
13 UIPopoverController and Modal View ControllerTom Fan
 
12 Camera
12 Camera12 Camera
12 CameraTom Fan
 

Plus de Tom Fan (20)

PhoneGap 通信原理和插件系统
PhoneGap 通信原理和插件系统PhoneGap 通信原理和插件系统
PhoneGap 通信原理和插件系统
 
HTML5 Web workers
HTML5 Web workersHTML5 Web workers
HTML5 Web workers
 
Web sockets
Web socketsWeb sockets
Web sockets
 
Storage
StorageStorage
Storage
 
Semantics
SemanticsSemantics
Semantics
 
Multimedia
MultimediaMultimedia
Multimedia
 
Intro to-html5
Intro to-html5Intro to-html5
Intro to-html5
 
Html5 history
Html5 historyHtml5 history
Html5 history
 
Geolocation
GeolocationGeolocation
Geolocation
 
File api
File apiFile api
File api
 
Deviceaccess
DeviceaccessDeviceaccess
Deviceaccess
 
Css3
Css3Css3
Css3
 
Webstorage
WebstorageWebstorage
Webstorage
 
Html5 最重要的部分
Html5 最重要的部分Html5 最重要的部分
Html5 最重要的部分
 
AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状
 
PhoneGap 2.0 开发
PhoneGap 2.0 开发PhoneGap 2.0 开发
PhoneGap 2.0 开发
 
Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发
 
HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型
 
13 UIPopoverController and Modal View Controller
13 UIPopoverController and Modal View Controller13 UIPopoverController and Modal View Controller
13 UIPopoverController and Modal View Controller
 
12 Camera
12 Camera12 Camera
12 Camera
 

Dernier

SymPy 在微積分上的應用_5.pptx SymPy 在微積分上的應用_5.pptx
SymPy 在微積分上的應用_5.pptx SymPy 在微積分上的應用_5.pptxSymPy 在微積分上的應用_5.pptx SymPy 在微積分上的應用_5.pptx
SymPy 在微積分上的應用_5.pptx SymPy 在微積分上的應用_5.pptxNCU MCL
 
函數畫圖_習題7.pptx 函數畫圖_習題7.pptx 函數畫圖_習題7.pptx
函數畫圖_習題7.pptx 函數畫圖_習題7.pptx 函數畫圖_習題7.pptx函數畫圖_習題7.pptx 函數畫圖_習題7.pptx 函數畫圖_習題7.pptx
函數畫圖_習題7.pptx 函數畫圖_習題7.pptx 函數畫圖_習題7.pptxNCU MCL
 
买假和真英国驾驶执照买了假的英国驾照,那跟真的有什么区别吗?买假和真正的澳大利亚驾驶执照【微信qoqoqdqd】
买假和真英国驾驶执照买了假的英国驾照,那跟真的有什么区别吗?买假和真正的澳大利亚驾驶执照【微信qoqoqdqd】买假和真英国驾驶执照买了假的英国驾照,那跟真的有什么区别吗?买假和真正的澳大利亚驾驶执照【微信qoqoqdqd】
买假和真英国驾驶执照买了假的英国驾照,那跟真的有什么区别吗?买假和真正的澳大利亚驾驶执照【微信qoqoqdqd】黑客 接单【TG/微信qoqoqdqd】
 
函數微分_習題4.pptx 函數微分_習題4.pptx 函數微分_習題4.pptx
函數微分_習題4.pptx 函數微分_習題4.pptx 函數微分_習題4.pptx函數微分_習題4.pptx 函數微分_習題4.pptx 函數微分_習題4.pptx
函數微分_習題4.pptx 函數微分_習題4.pptx 函數微分_習題4.pptxNCU MCL
 
20170104 - transaction_pattern
20170104 - transaction_pattern20170104 - transaction_pattern
20170104 - transaction_patternJamie (Taka) Wang
 
20211119 - demystified artificial intelligence with NLP
20211119 - demystified artificial intelligence with NLP20211119 - demystified artificial intelligence with NLP
20211119 - demystified artificial intelligence with NLPJamie (Taka) Wang
 
20161220 - domain-driven design
20161220 - domain-driven design20161220 - domain-driven design
20161220 - domain-driven designJamie (Taka) Wang
 
函數畫圖_習題5.pptx 函數畫圖_習題5.pptx 函數畫圖_習題5.pptx
函數畫圖_習題5.pptx 函數畫圖_習題5.pptx 函數畫圖_習題5.pptx函數畫圖_習題5.pptx 函數畫圖_習題5.pptx 函數畫圖_習題5.pptx
函數畫圖_習題5.pptx 函數畫圖_習題5.pptx 函數畫圖_習題5.pptxNCU MCL
 
函數畫圖_習題6.pptx 函數畫圖_習題6.pptx 函數畫圖_習題6.pptx
函數畫圖_習題6.pptx 函數畫圖_習題6.pptx 函數畫圖_習題6.pptx函數畫圖_習題6.pptx 函數畫圖_習題6.pptx 函數畫圖_習題6.pptx
函數畫圖_習題6.pptx 函數畫圖_習題6.pptx 函數畫圖_習題6.pptxNCU MCL
 
SymPy 在微積分上的應用_4.pptx SymPy 在微積分上的應用_4.pptx
SymPy 在微積分上的應用_4.pptx SymPy 在微積分上的應用_4.pptxSymPy 在微積分上的應用_4.pptx SymPy 在微積分上的應用_4.pptx
SymPy 在微積分上的應用_4.pptx SymPy 在微積分上的應用_4.pptxNCU MCL
 

Dernier (15)

SymPy 在微積分上的應用_5.pptx SymPy 在微積分上的應用_5.pptx
SymPy 在微積分上的應用_5.pptx SymPy 在微積分上的應用_5.pptxSymPy 在微積分上的應用_5.pptx SymPy 在微積分上的應用_5.pptx
SymPy 在微積分上的應用_5.pptx SymPy 在微積分上的應用_5.pptx
 
20200226 - AI Overview
20200226 - AI Overview20200226 - AI Overview
20200226 - AI Overview
 
函數畫圖_習題7.pptx 函數畫圖_習題7.pptx 函數畫圖_習題7.pptx
函數畫圖_習題7.pptx 函數畫圖_習題7.pptx 函數畫圖_習題7.pptx函數畫圖_習題7.pptx 函數畫圖_習題7.pptx 函數畫圖_習題7.pptx
函數畫圖_習題7.pptx 函數畫圖_習題7.pptx 函數畫圖_習題7.pptx
 
买假和真英国驾驶执照买了假的英国驾照,那跟真的有什么区别吗?买假和真正的澳大利亚驾驶执照【微信qoqoqdqd】
买假和真英国驾驶执照买了假的英国驾照,那跟真的有什么区别吗?买假和真正的澳大利亚驾驶执照【微信qoqoqdqd】买假和真英国驾驶执照买了假的英国驾照,那跟真的有什么区别吗?买假和真正的澳大利亚驾驶执照【微信qoqoqdqd】
买假和真英国驾驶执照买了假的英国驾照,那跟真的有什么区别吗?买假和真正的澳大利亚驾驶执照【微信qoqoqdqd】
 
20161027 - edge part2
20161027 - edge part220161027 - edge part2
20161027 - edge part2
 
函數微分_習題4.pptx 函數微分_習題4.pptx 函數微分_習題4.pptx
函數微分_習題4.pptx 函數微分_習題4.pptx 函數微分_習題4.pptx函數微分_習題4.pptx 函數微分_習題4.pptx 函數微分_習題4.pptx
函數微分_習題4.pptx 函數微分_習題4.pptx 函數微分_習題4.pptx
 
20200323 - AI Intro
20200323 - AI Intro20200323 - AI Intro
20200323 - AI Intro
 
20170104 - transaction_pattern
20170104 - transaction_pattern20170104 - transaction_pattern
20170104 - transaction_pattern
 
20211119 - demystified artificial intelligence with NLP
20211119 - demystified artificial intelligence with NLP20211119 - demystified artificial intelligence with NLP
20211119 - demystified artificial intelligence with NLP
 
20161220 - domain-driven design
20161220 - domain-driven design20161220 - domain-driven design
20161220 - domain-driven design
 
函數畫圖_習題5.pptx 函數畫圖_習題5.pptx 函數畫圖_習題5.pptx
函數畫圖_習題5.pptx 函數畫圖_習題5.pptx 函數畫圖_習題5.pptx函數畫圖_習題5.pptx 函數畫圖_習題5.pptx 函數畫圖_習題5.pptx
函數畫圖_習題5.pptx 函數畫圖_習題5.pptx 函數畫圖_習題5.pptx
 
函數畫圖_習題6.pptx 函數畫圖_習題6.pptx 函數畫圖_習題6.pptx
函數畫圖_習題6.pptx 函數畫圖_習題6.pptx 函數畫圖_習題6.pptx函數畫圖_習題6.pptx 函數畫圖_習題6.pptx 函數畫圖_習題6.pptx
函數畫圖_習題6.pptx 函數畫圖_習題6.pptx 函數畫圖_習題6.pptx
 
20151111 - IoT Sync Up
20151111 - IoT Sync Up20151111 - IoT Sync Up
20151111 - IoT Sync Up
 
Entities in DCPS (DDS)
Entities in DCPS (DDS)Entities in DCPS (DDS)
Entities in DCPS (DDS)
 
SymPy 在微積分上的應用_4.pptx SymPy 在微積分上的應用_4.pptx
SymPy 在微積分上的應用_4.pptx SymPy 在微積分上的應用_4.pptxSymPy 在微積分上的應用_4.pptx SymPy 在微積分上的應用_4.pptx
SymPy 在微積分上的應用_4.pptx SymPy 在微積分上的應用_4.pptx
 

05 MapKit and Text Input

  • 2. • 在这⼀一章,我们将使⽤用 MapKit 框架,UITextField 类以及更多的 delegation 来完成 Whereami 应⽤用。 • ⺫⽬目前,whereami 找到位置并且在控制台打印出 来。这章结束时,应⽤用程序将显⽰示出当前位置的 地图.并且⽤用户还有⼀一个可选项,可以使⽤用MapKit annotation 标记和命名当前的位置。默认的 MapKit annotation 显⽰示为地图上的⼀一个红⾊色的⼤大 头针。
  • 5. view objects • MKAnnotationView • 在 MKMapView 上作为图标显⽰示的⼏几个 MKAnnotationView 的实例 • MKMapView • 显⽰示地图,和所记录的位置的标签 • UIActivityIndicatorView • 表⽰示当前设备还在⼯工作,没有⽌止步不前 • UITextField • 允许⽤用户输⼊入⽂文本来给地图上的当前位置打标签
  • 6. model objects • CLLocationManager • 和设备交互以确定⽤用户位置 • BNRMapPoint • 我们⾃自⼰己创建的模型对象
  • 7. controller object • WhereamiViewController • 负责处理来⾃自其他对象的更新和请求 • 是 MKMapView 的 delegate • 当⼀一个 view 或者多个 views 被添加上以后 MKMapView 会发 送 mapView:didAddAnnotationViews: • 是 UITextField 的 delegate • ⽤用户完成输⼊入⽂文本后,UITextField 会发送 textFieldShouldReturn: • 是 CLLocationManager 的 delegate • 发送 locationManager:didUpdateLocations: 通知 WhereamiViewController 位置已经更新
  • 8. MapKit 框架 • Core Location framework 告诉我们我们在世界上 的什么地⽅方 • MapKit framework 把世界显⽰示给我们 • MapKit 的主要⼯工作都是由 MKMapView 类完成的 • 显⽰示⼀一个地图 • 追踪触控 • 以及显⽰示注释(annotation)
  • 9. 把 MapKit framework 加⼊入项⺫⽬目
  • 10. 导⼊入 MapKit 头⽂文件 #import <UIKit/UIKit.h> // 导⼊入 Core Location framework 的头⽂文件 #import <CoreLocation/CoreLocation.h> // 导⼊入MapKit framework 的头⽂文件 #import <MapKit/MapKit.h>
  • 11. 声明必要的实例变量 @interface WhereamiViewController : UIViewController <CLLocationManagerDelegate> { CLLocationManager *locationManager; IBOutlet MKMapView *worldView; IBOutlet UIActivityIndicatorView *activityIndicator; IBOutlet UITextField *locationTitleField; } @end
  • 12. 界⾯面属性(Interface Properties) 1.从界⾯面右侧的 object library 中拖拽 MKMapView 到 UIView 上 2.再拖拽 UITextField, UIActivityIndicatorView 到 MKMapView 上,适当调整位置和⼤大⼩小 3.设置 ViewController 中实例变量到新增加的视图的 链接(IBOutlet指⽰示的) 4.设置新增加的视图的 delegate 到 ViewController
  • 15. 调整 UITextField 属性 • 设置 Placeholder • 设置 Return Key
  • 16. 设置 UIActivityIndicator 属性 • 不转的时候⾃自动隐藏 • 勾选上“Hides When Stopped”
  • 17. 作为 MapView 的 Delegate • 我们希望 Whereami 启动时,找到当前的位置并在 地图上显⽰示 • 上⼀一个主题我们直接使⽤用 Core Location 查找⽤用户 的位置 • MKMapView 实例本⾝身知道如何使⽤用 Core Location 查找⽤用户位置 • 设置 MKMapView 的 showUserLocation 属性为 YES, 然后它就会查找⽤用户的位置并把它显⽰示在地图上 • 界⾯面加载完以后,WhereamiViewController 会被发送 viewDidLoad 消息,我们在这⾥里⾯面告诉 MKMapView 来 更新它的位置。
  • 18. 修改 WhereamiViewController.m - (void)viewDidLoad { [super viewDidLoad]; ! // Do any additional setup after loading the view, typically from a nib. [worldView setShowsUserLocation:YES]; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:self]; [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; [locationManager startUpdatingLocation]; } return self; }
  • 20. ⽂文档 • API Reference • System Guides • Tools Guides • Sample Code
  • 21. mapView:didUpdateUserLocation • 通知⽤用户位置更新 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { }
  • 23. MKCoordinateRegion typedef struct { ! CLLocationCoordinate2D center; ! MKCoordinateSpan span; } MKCoordinateRegion; typedef struct { ! CLLocationDegrees latitude; ! CLLocationDegrees longitude; } CLLocationCoordinate2D; typedef struct { CLLocationDegrees latitudeDelta; CLLocationDegrees longitudeDelta; } MKCoordinateSpan; typedef double CLLocationDegrees;
  • 24. MKUserLocation @interface MKUserLocation : NSObject <MKAnnotation> { @private MKUserLocationInternal *_internal; } // Returns YES if the user's location is being updated. @property (readonly, nonatomic, getter=isUpdating) BOOL updating; // Returns nil if the owning MKMapView's showsUserLocation is NO or the user's location has yet to be determined. @property (readonly, retain, nonatomic) CLLocation *location; // Returns nil if not in MKUserTrackingModeFollowWithHeading @property (readonly, nonatomic, retain) CLHeading *heading NS_AVAILABLE(NA, 5_0); // The title to be displayed for the user location annotation. @property (nonatomic, copy) NSString *title; // The subtitle to be displayed for the user location annotation. @property (nonatomic, copy) NSString *subtitle; @end
  • 25. MKAnnotation @protocol MKAnnotation <NSObject> // Center latitude and longitude of the annotion view. // The implementation of this property must be KVO compliant. @property (nonatomic, readonly) CLLocationCoordinate2D coordinate; @optional // Title and subtitle for use by selection UI. @property (nonatomic, readonly, copy) NSString *title; @property (nonatomic, readonly, copy) NSString *subtitle; // Called as a result of dragging an annotation view. - (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate NS_AVAILABLE(NA, 4_0); @end
  • 26. MKAnnotation • MKAnnotation 不是 delegate protocol • 声明了⼀一套对于任何想要把⾃自⼰己的实例显⽰示到 map view 上的类⾮非常有⽤用的⽅方法 • ⽐比⽅方附近的酒店,⼯工⼚厂,⽕火⻋车站.... 这些类在应⽤用 程序中都是⾮非常不同并且层次⽆无关的,但是只要 他们满⾜足 MKAnnotation, 它们都可以被添加到 MKMapView
  • 27. MKAnnotationView • 当⼀一个满⾜足 MKAnnotation 的对象被添加到 MKMapView 时,⼀一个MKAnnotationView 的实例 会被创建并添加到 map view • 这个 MKAnnotationView 持有⼀一个到这个对象的指 针,以便需要的时候可以向它请求数据
  • 29. BNRMapPoint • 新建⼀一个类 BNRMapPoint, 让它遵守 MKAnnotation • 声明两个属性和⼀一个初始化器
  • 30. BNRMapPoint.h #import <Foundation/Foundation.h> #import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> @interface BNRMapPoint : NSObject <MKAnnotation> { } -(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t; @property (nonatomic, readonly) CLLocationCoordinate2D coordinate; @property (nonatomic, copy) NSString *title; @end
  • 31. BNRMapPoint.m @implementation BNRMapPoint @synthesize coordinate, title; - (id)init { return [self initWithCoordinate:CLLocationCoordinate2DMake(39.91503, 116.4631) title:@"央视新台址"]; } - (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t { self = [super init]; if (self) { coordinate = c; [self setTitle:t]; } return self; } @end
  • 32. 给位置打标签 • 现在我们有了⼀一个符合 MKAnnotation 的类 (BNRMapPoint),我们可以把它的实例显⽰示在 地图上 • ⽤用户可以在 UITextField 中输⼊入位置的名称,然后 按下 keyboard 上的 Done 按钮 • 轻按 Done 是增加⼀一个 annotation 的信号,怎么 才能知道这个事件发⽣生了呢? 还是 Delegation
  • 33. textFieldShouldReturn @interface WhereamiViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate> - (BOOL)textFieldShouldReturn:(UITextField *)textField { [self findLocation]; [textField resignFirstResponder]; return YES; }
  • 34. first responder • UIResponder 是 UIKit framework 的⼀一个类,⼀一个 responder 负责接收和处理与其相关的事件 • UIView 是 UIResponder 的⼦子类,因此 UIView 对象 可以接收事件 • 按钮是可以处理触控事件的 responder,像 tap • 晃动设备及点击键盘上的⼀一个键也会产⽣生事件 • responders 之⼀一就是 first responder, 同⼀一时间只能 有⼀一个 responder 可以作为 first responder • first responder 处理那些不和屏幕上某⼀一具体位置 相关的事件。举例:tap 和 shaking的区别
  • 35. resignFirstResponder • UITextField 也是⼀一个 responder:它是 UIControl 的直接⼦子类,UIControl 是 UIView 的⼦子类,UIView 是 UIResponder 的⼦子类 • 当UITextField 被点击的时候,它通过变成 first responder 来处理事件 • 当 UITextField 变成 first responder, ⼀一个键盘就会 ⾃自动出现在屏幕上;要从屏幕上移除键盘,要通 过给它发送 resignFirstResponder 消息来告诉 UITextField 放弃它的 first responder 状态,⼀一旦 first responder 不再是 UITextField,键盘就会消失
  • 37. <> 和 “” • 和 C++ ⼀一样,从框架中包含的⽂文件使⽤用<>尖括 号,⾃自⼰己编写的头⽂文件使⽤用“”引号 • #import <MapKit/MapKit.h>,表⽰示仅在系统库下 ⾯面查找这个⽂文件 • #import "BNRMapPoint.h",表⽰示⾸首先在项⺫⽬目⽂文件 下查找,找不到的话再在系统库下⾯面查找