萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> 在iOS中進行衛星定位的實例編程

在iOS中進行衛星定位的實例編程

本文導航

1、首頁2、iOS中進行衛星定位-2

想用你的iPhone 顯示你現在的位置嗎?現在就看看小編為大家整理的iOS中進行衛星定位的實例編程吧!

綱要:

- 在程序顯示前運行代碼 -

- 加入CoreLocation Frameworks -

- 關於iPhone的“Utility Application” 運用 -

- CLLocationManager 代碼運用 -

首先運行以安裝好的 xCode

選擇: File->New Project.

從 "New Project" 窗口

選擇 : iPhone OS ->Applications-> Utility Application

命名 : 我這裡命名為 “WhereAmI”

(1)  在Project文件上右鍵點擊 Linked Frameworks and Libraries->"+"->Existing Framework;在Frameworks文件夾下選擇 CoreLocation.framework, 按Add

#FormatImgID_0#

 

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

#import

#import

#import

@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 內填上"經度"

iOS中進行衛星定位

(6) 加入 Text Field ; 顯示:經度

選擇: Tools -> Library ; 從Library顯示菜單中拖拉一個 Text Field 到 Main View

在主視窗口或文件窗口;點擊 Text Field

選擇: Tools -> Connection Inspector

移動鼠標在"Referencing Outlets" 後面圓圈上; 圓圈變為(+); 拖向直線連接到"Main View";

放開鼠標選擇鍵出現 "longitude"; 選上它。

copyright © 萬盛學電腦網 all rights reserved