iPhone GPS定位系統(tǒng) 實(shí)例操作
iPhone GPS定位系統(tǒng) 實(shí)例操作是本文要介紹的內(nèi)容,先來(lái)看本文詳細(xì)內(nèi)容。如今,配備GPS功能的移動(dòng)設(shè)備越來(lái)越普遍了,使用GPS定位系統(tǒng),可以精確地定位你當(dāng)前所在的地理位置,但由于GPS接收機(jī)需要對(duì)準(zhǔn)天空才能工作,因此在室內(nèi)環(huán)境基本無(wú)用。
另一個(gè)找到自己所在位置的有效方法是使用手機(jī)基站,手機(jī)開(kāi)機(jī)時(shí),它會(huì)與周?chē)幕颈3致?lián)系,如果你知道這些基站的身份,就可以使用各種數(shù)據(jù)庫(kù) (包含基站的身份和它們的確切地理位置)計(jì)算出手機(jī)的物理位置。基站不需要衛(wèi)星,和GPS不同,它對(duì)室內(nèi)環(huán)境一樣管用。但它沒(méi)有GPS那樣精確,它的精度取決于基站的密度,它在基站密集型區(qū)域的準(zhǔn)確度最高。
提示:第一代iPhone并沒(méi)有配置GPS接收器,基站方式不能應(yīng)用到iPod Touch上,因?yàn)樗皇鞘謾C(jī)。
第三種方法是依賴(lài)Wi-Fi,使用這種方法時(shí),設(shè)備連接到Wi-Fi網(wǎng)絡(luò),通過(guò)檢查服務(wù)提供商的數(shù)據(jù)確定位置,它既不依賴(lài)衛(wèi)星,也不依賴(lài)基站,因此這個(gè)方法對(duì)于可以連接到Wi-Fi網(wǎng)絡(luò)的區(qū)域有效,但它的精確度也是這三個(gè)方法中最差的。
定位框架內(nèi)核
在iPhone上,蘋(píng)果提供了定位框架內(nèi)核,以幫助你確定你的物理位置,這個(gè)框架的美妙之處在于它使用了前面提到的所有三種方法,具體使用的是哪種方法對(duì)于開(kāi)發(fā)者來(lái)說(shuō)是透明的,開(kāi)發(fā)人員只需要指定所需要的精度,定位內(nèi)核將會(huì)以最佳方式確定定位結(jié)果。
你一定感到很吃驚吧?!本文其余部分將向你展示這是如何做到的。
獲取位置坐標(biāo)
使用Xcode,創(chuàng)建一個(gè)新的基于視圖的應(yīng)用程序項(xiàng)目,取名為L(zhǎng)BS,在新項(xiàng)目中,雙擊LBSViewController.xib文件,在界面設(shè)計(jì)工具中編輯它。使用下面的組件填充視圖窗口,如圖1所示。
Label
TextField
圖 1 位置視圖實(shí)例:用Label和TextFiled填充這個(gè)窗口
在Xcode中框架組上點(diǎn)擊右鍵,選擇“添加”*“現(xiàn)有框架”,選擇 “Framework/CoreLocation.framework”,向LBSViewController.h文件中添加以下粗體字顯示的代碼:
- #import
- #import
- @interface LBSViewController : UIViewController
- {
- IBOutlet UITextField *latitudeTextField;
- IBOutlet UITextField *longitudeTextField;
- IBOutlet UITextField *accuracyTextField;
- CLLocationManager *lm;
- }
- @property (retain, nonatomic) UITextField *latitudeTextField;
- @property (retain, nonatomic) UITextField *longitudeTextField;
- @property (retain, nonatomic) UITextField *accuracyTextField;
- @end
若要使用CLLocationManager類(lèi),需要在你的視圖控制器類(lèi)中實(shí)現(xiàn)CLLocationManagerDelegate協(xié)議,還需要?jiǎng)?chuàng)建三個(gè)出口用于連接視圖窗口中的三個(gè)TextFiled視圖。
回到界面編輯器,單擊并拖動(dòng)文檔的所有者項(xiàng)目到三個(gè)TextField視圖,然后分別選擇latitudeTextField,longitudeTextField和accuracyTextField。
在 LBSViewController.m文件中查詢(xún)以下代碼中的粗體部分:
- #import "LBSViewController.h"
- @implementation LBSViewController
- @synthesize latitudeTextField, longitudeTextField, accuracyTextField;
- - (void) viewDidLoad {
- lm = [[CLLocationManager alloc] init];
- if ([lm locationServicesEnabled]) {
- lm.delegate = self;
- lm.desiredAccuracy = kCLLocationAccuracyBest;
- lm.distanceFilter = 1000.0f;
- [lm startUpdatingLocation];
- }
- }
- - (void) locationManager: (CLLocationManager *) manager
- didUpdateToLocation: (CLLocation *) newLocation
- fromLocation: (CLLocation *) oldLocation{
- NSString *lat = [[NSString alloc] initWithFormat:@"%g",
- newLocation.coordinate.latitude];
- latlatitudeTextField.text = lat;
- NSString *lng = [[NSString alloc] initWithFormat:@"%g",
- newLocation.coordinate.longitude];
- longitudeTextField.text = lng;
- NSString *acc = [[NSString alloc] initWithFormat:@"%g",
- newLocation.horizontalAccuracy];
- accaccuracyTextField.text = acc;
- [acc release];
- [lat release];
- [lng release]
- }
- - (void) locationManager: (CLLocationManager *) manager
- didFailWithError: (NSError *) error {
- NSString *msg = [[NSString alloc]
- initWithString:@"Error obtaining location"];
- UIAlertView *alert = [[UIAlertView alloc]
- initWithTitle:@"Error"
- message:msg
- delegate:nil
- cancelButtonTitle: @"Done"
- otherButtonTitles:nil];
- [alert show];
- [msg release];
- [alert release];
- }
- (void) dealloc{
- [lm release];
- [latitudeTextField release];
- [longitudeTextField release];
- [accuracyTextField release];
- [super dealloc];
- }
前面的代碼創(chuàng)建了 CLLocationManager類(lèi)的一個(gè)實(shí)例,在使用對(duì)象之前,你應(yīng)該檢查用戶(hù)是否開(kāi)啟了設(shè)備的定位服務(wù),如果開(kāi)啟了,你可以使用 desiredAccuracy屬性指定想要的精度,使用下面的常量指定想要的精度:
- kCLLocationAccuracyBest
- kCLLocationAccuracyNearestTenMeters
- kCLLocationAccuracyHundredMeters
- kCLLocationAccuracyKilometer
- kCLLocationAccuracyThreeKilometers
distanceFilter屬性讓你指定設(shè)備必須移動(dòng)多少距離位置信息才會(huì)更新,這個(gè)屬性的單位是米。如果你想得到所有移動(dòng)的通知,可以使用kCLDistanceFilterNone常量,最后,使用 startUpdatingLocation方法啟動(dòng)位置管理器。
要獲得位置信息,需處理下面兩個(gè)事件:
- locationManager:didUpdateToLocation:fromLocation:
- locationManager:didFailWithError:
當(dāng)獲得一個(gè)新的定位值時(shí),設(shè)備觸發(fā) locationManager:didUpdateToLocation:fromLocation:事件,如果位置管理器不能確定位置信息,就會(huì)觸發(fā) locationManager:didFailWithError:事件。
當(dāng)設(shè)備可以確定位置時(shí),你可能想顯示經(jīng)緯度值和精度,這時(shí)你可以使用CLLocation對(duì)象,它的horizontalAccuracy屬性可以指定精度范圍,單位是米。
按Command- r在iPhone模擬器上測(cè)試該程序,圖2顯示了模擬器顯示的位置經(jīng)緯度值,同時(shí)顯示了精度。
圖 2 定位測(cè)試:當(dāng)你在iPhone模擬器上測(cè)試該示例程序時(shí),總會(huì)顯示這些固定的值
顯示地圖
如果能將位置坐標(biāo)定位到地圖上顯示將會(huì)更有趣,幸運(yùn)的是,iPhone 3.0 SDK包括了Map Kit API,它可以讓你在程序中顯示Google Map,下面以一個(gè)例子進(jìn)行說(shuō)明。
還是使用前面創(chuàng)建的項(xiàng)目,在 LBSViewController.xib文件中視圖窗口上增加一個(gè)按鈕,如圖3所示。
圖 3 View Map按鈕:增加按鈕后的樣子
在Xcode中框架組上點(diǎn)擊右鍵,增加一個(gè)新的框架MapKit.framework。在 LBSViewController.h文件中添加下列代碼中的粗體部分:
- #import
- #import
- #import
- @interface LBSViewController : UIViewController
- {
- IBOutlet UITextField *accuracyTextField;
- IBOutlet UITextField *latitudeTextField;
- IBOutlet UITextField *longitudeTextField;
- CLLocationManager *lm;
- MKMapView *mapView;
- }
- @property (retain, nonatomic) UITextField *accuracyTextField;
- @property (retain, nonatomic) UITextField *latitudeTextField;
- @property (retain, nonatomic) UITextField *longitudeTextField;
- -(IBAction) btnViewMap: (id) sender;
- @end
回到界面編輯器,拖動(dòng)按鈕到文件的所有者項(xiàng)目上,然后選擇btnViewMap:。
在LBSViewController.m文件中,添加下列代碼中的粗體部分:
- -(IBAction) btnViewMap: (id) sender {
- [self.view addSubview:mapView];
- }
- (void) viewDidLoad {
- lm = [[CLLocationManager alloc] init];
- lm.delegate = self;
- lm.desiredAccuracy = kCLLocationAccuracyBest;
- lm.distanceFilter = 1000.0f;
- [lm startUpdatingLocation];
- mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
- mapView.mapType = MKMapTypeHybrid;
- }
- - (void) locationManager: (CLLocationManager *) manager
- didUpdateToLocation: (CLLocation *) newLocation
- fromLocation: (CLLocation *) oldLocation{
- NSString *lat = [[NSString alloc] initWithFormat:@"%g",
- newLocation.coordinate.latitude];
- latlatitudeTextField.text = lat;
- NSString *lng = [[NSString alloc] initWithFormat:@"%g",
- newLocation.coordinate.longitude];
- longitudeTextField.text = lng;
- NSString *acc = [[NSString alloc] initWithFormat:@"%g",
- newLocation.horizontalAccuracy];
- accaccuracyTextField.text = acc;
- [acc release];
- [lat release];
- [lng release];
- MKCoordinateSpan span;
- span.latitudeDelta=.005;
- span.longitudeDelta=.005;
- MKCoordinateRegion region;
- region.center = newLocation.coordinate;
- region.span=span;
- [mapView setRegion:region animated:TRUE];
- }
- - (void) dealloc{
- [mapView release];
- [lm release];
- [latitudeTextField release];
- [longitudeTextField release];
- [accuracyTextField release];
- [super dealloc];
- }
代碼解釋?zhuān)?/p>
當(dāng)視圖載入時(shí)創(chuàng)建一個(gè)MKMapView類(lèi)的實(shí)例,設(shè)置顯示的地圖類(lèi)型。
當(dāng)用戶(hù)點(diǎn)擊View Map按鈕時(shí),在當(dāng)前視圖上增加mapView對(duì)象。
當(dāng)位置信息得到更新時(shí),使用mapView對(duì)象的setRegion:方法放大地圖。
在iPhone模擬器中按Command-r測(cè)試該程序,點(diǎn)擊View Map按鈕將會(huì)顯示一個(gè)包含位置管理器返回位置的地圖。如圖4所示。
圖 4 地圖位置:通過(guò)定位內(nèi)核框架顯示地圖位置
因?yàn)槟M器始終顯示的是相同的位置,如果你有一部iPhone手機(jī)也可以真實(shí)地感受一下,當(dāng)你移動(dòng)位置時(shí),你會(huì)看到地圖會(huì)自動(dòng)更新。將 distanceFilter屬性設(shè)置得小一點(diǎn),這樣可以增強(qiáng)跟蹤體驗(yàn)。
正如你所看到的,iPhone SDK的定位內(nèi)核框架讓你可以很容易實(shí)現(xiàn)基于位置的設(shè)備,此外,MapKit(包括在iPhone SDK中)可以在地圖上顯示位置信息。如果沒(méi)有創(chuàng)建過(guò)基于位置的應(yīng)用,我想你看完本文就應(yīng)該動(dòng)手一試,還等什么呢? via:http://ming-fanglin.javaeye.com/blog/703744
小結(jié):iPhone GPS定位系統(tǒng) 實(shí)例操作的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!更多相關(guān)內(nèi)容請(qǐng)參考編輯推薦!
本篇文章來(lái)源于 黑軟基地-中國(guó)最大的黑客軟件安全教程下載站!(手機(jī)資訊) 原文鏈接:http://www.hackvip.com/mobiwen/html/Mobile_220397_3.html