DAY
今天来建立一个 iPhone app 软件,用你的iPhone 显示你现在的位置。
纲要:
- 在程序显示前运行代码 -
- 加入CoreLocation Frameworks -
- 关于iPhone的“Utility Application” 运用 -
- CLLocationManager 代码运用 -
首先运行以安装好的 xCode
选择: File->New Project.
从 "New Project" 窗口
选择 : iPhone OS ->Applications-> Utility Application
命名 : 我这里命名为 “WhereAmI”
(1)
(2)
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreLocation/CLLocationManagerDelegat
@interface MainView : UIView {
}
- (IBAction)update;
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *) oldLocation ;
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *) error;
@end
(3)
#import "MainView.h"
@implementation MainView
- (IBAction)update {
}
CLLocationManager* locmanager;
-(void)awakeFromNib {
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
}
- (id)initWithFrame:(CGRect)frame {
}
- (void)drawRect:(CGRect)rect {
}
- (void)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.