实例iPhone编程入门教程-第十天

DAY TEN – WhereAmI

今天来建立一个 iPhone app 软件,用你的iPhone 显示你现在的位置。


纲要:
- 在程序显示前运行代码 -
- 加入CoreLocation Frameworks -
- 关于iPhone的“Utility Application” 运用 -
- CLLocationManager 代码运用 -


首先运行以安装好的 xCode

选择: File->New Project.

从 "New Project" 窗口
 
选择 : iPhone OS ->Applications-> Utility Application
命名 : 我这里命名为 “WhereAmI”

 

(1)  在xCode右键点击 Frameworks ->Add->Existing Framework;在Frameworks文件夹下选择 CoreLocation.framework, 按Add




 

(2)  在xCode打开 MainView.h 文件,加入下面代码

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreLocation/CLLocationManagerDelegate.h>

@interface MainView : UIView {
    IBOutlet UITextField *altitude;
    IBOutlet UITextField *latitude;
    IBOutlet UITextField *longitude;
 
 CLLocationManager   *locmanager;
 BOOL                wasFound;
}

- (IBAction)update;

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

@end

 

(3)  在xCode打开 MainView.m 文件,加入下面代码
#import "MainView.h"

@implementation MainView

- (IBAction)update {
 
 locmanager = [[CLLocationManager alloc] init];
 [locmanager setDelegate:self];
 [locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
 
 [locmanager startUpdatingLocation];
}

CLLocationManager* locmanager;

-(void)awakeFromNib {
 [self update];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
 if (wasFound) return;
 wasFound = YES;
 
 CLLocationCoordinate2D loc = [newLocation coordinate];
 
 latitude.text = [NSString stringWithFormat: @"%f", loc.latitude];
 longitude.text = [NSString stringWithFormat: @"%f", loc.longitude];
 altitude.text = [NSString stringWithFormat: @"%f", newLocation.altitude];
 
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
 
}


- (id)initWithFrame:(CGRect)frame {
 if (self = [super initWithFrame:frame]) {
  // Initialization code
 }
 return self;
}


- (void)drawRect:(CGRect)rect {
 // Drawing code
}


- (void)dealloc {
 [super dealloc];
}

 

(4) UIView 界面设置

双击文件: "main.xib" ;
然后 "Interface Builder"  会自动打开,在这里我们可以编辑改变界面

 

(5) 加入Label 在 Attributes 下, Text 内填上"经度"



(6) 加入 Text Field ; 显示:经度

选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Text Field 到 Main View
在主视窗口或文件窗口;点击 Text Field

选择: Tools -> Connection Inspector

移动鼠标在"Referencing Outlets" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"Main View";
放开鼠标选择键出现 "longitude"; 选上它。


(7) 加入Label 在 Attributes 下, Text 内填上"纬度"



(8) 加入 Text Field ; 显示:纬度

选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Text Field 到 Main View
在主视窗口或文件窗口;点击 Text Field

选择: Tools -> Connection Inspector

移动鼠标在"Referencing Outlets" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"Main View";
放开鼠标选择键出现 "latitude"; 选上它。

 

(9) 加入Label 在 Attributes 下, Text 内填上"海拔".



(10) 加入 Text Field ; 显示:海拔

选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Text Field 到 Main View
在主视窗口或文件窗口;点击 Text Field

选择: Tools -> Connection Inspector

移动鼠标在"Referencing Outlets" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"Main View";
放开鼠标选择键出现 "altitude"; 选上它。


(11) 加入 UpDate Button 在 Attributes 下, Text 内填上"Update".

选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Button 到 Main View
在主视窗口或文件窗口;点击 Button

选择: Tools -> Connection Inspector

移动鼠标在"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"Main View";
放开鼠标选择键出现 "Update"; 选上它。


最后在 xCode 选择 Build->Build and Go; Save All.

 

下载今天教程文件: WhereAmI.zip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值