自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone GPS定位系統(tǒng) 實例操作

移動開發(fā) iOS
本文介紹的是iPhone GPS定位系統(tǒng) 實例操作,GPS越來越普遍使用了,那么本文講述的主要是在iphone手機中的使用,先來看內(nèi)容。

iPhone GPS定位系統(tǒng) 實例操作是本文要介紹的內(nèi)容,先來看本文詳細內(nèi)容。如今,配備GPS功能的移動設(shè)備越來越普遍了,使用GPS定位系統(tǒng),可以精確地定位你當前所在的地理位置,但由于GPS接收機需要對準天空才能工作,因此在室內(nèi)環(huán)境基本無用。

另一個找到自己所在位置的有效方法是使用手機基站,手機開機時,它會與周圍的基站保持聯(lián)系,如果你知道這些基站的身份,就可以使用各種數(shù)據(jù)庫 (包含基站的身份和它們的確切地理位置)計算出手機的物理位置?;静恍枰l(wèi)星,和GPS不同,它對室內(nèi)環(huán)境一樣管用。但它沒有GPS那樣精確,它的精度取決于基站的密度,它在基站密集型區(qū)域的準確度最高。

提示:第一代iPhone并沒有配置GPS接收器,基站方式不能應(yīng)用到iPod Touch上,因為它不是手機。

第三種方法是依賴Wi-Fi,使用這種方法時,設(shè)備連接到Wi-Fi網(wǎng)絡(luò),通過檢查服務(wù)提供商的數(shù)據(jù)確定位置,它既不依賴衛(wèi)星,也不依賴基站,因此這個方法對于可以連接到Wi-Fi網(wǎng)絡(luò)的區(qū)域有效,但它的精確度也是這三個方法中最差的。

定位框架內(nèi)核

在iPhone上,蘋果提供了定位框架內(nèi)核,以幫助你確定你的物理位置,這個框架的美妙之處在于它使用了前面提到的所有三種方法,具體使用的是哪種方法對于開發(fā)者來說是透明的,開發(fā)人員只需要指定所需要的精度,定位內(nèi)核將會以最佳方式確定定位結(jié)果。

你一定感到很吃驚吧?!本文其余部分將向你展示這是如何做到的。

獲取位置坐標

使用Xcode,創(chuàng)建一個新的基于視圖的應(yīng)用程序項目,取名為LBS,在新項目中,雙擊LBSViewController.xib文件,在界面設(shè)計工具中編輯它。使用下面的組件填充視圖窗口,如圖1所示。

 Label

TextField

iPhone GPS定位系統(tǒng) 實例操作

圖 1 位置視圖實例:用Label和TextFiled填充這個窗口

在Xcode中框架組上點擊右鍵,選擇“添加”*“現(xiàn)有框架”,選擇 “Framework/CoreLocation.framework”,向LBSViewController.h文件中添加以下粗體字顯示的代碼:

  1.   #import   
  2.   #import   
  3.   @interface LBSViewController : UIViewController  
  4.    {  
  5.   IBOutlet UITextField *latitudeTextField;  
  6.     
  7.   IBOutlet UITextField *longitudeTextField;  
  8.   IBOutlet UITextField *accuracyTextField;  
  9.   CLLocationManager *lm;  
  10.   }  
  11.   @property (retain, nonatomic) UITextField *latitudeTextField;  
  12.   @property (retain, nonatomic) UITextField *longitudeTextField;  
  13.   @property (retain, nonatomic) UITextField *accuracyTextField;  
  14.   @end 

若要使用CLLocationManager類,需要在你的視圖控制器類中實現(xiàn)CLLocationManagerDelegate協(xié)議,還需要創(chuàng)建三個出口用于連接視圖窗口中的三個TextFiled視圖。

回到界面編輯器,單擊并拖動文檔的所有者項目到三個TextField視圖,然后分別選擇latitudeTextField,longitudeTextField和accuracyTextField。

在 LBSViewController.m文件中查詢以下代碼中的粗體部分:

  1.   #import "LBSViewController.h"  
  2.   @implementation LBSViewController  
  3.   @synthesize latitudeTextField, longitudeTextField, accuracyTextField;  
  4.   - (void) viewDidLoad {  
  5.   lm = [[CLLocationManager alloc] init];  
  6.   if ([lm locationServicesEnabled]) {  
  7.   lm.delegate = self;  
  8.   lm.desiredAccuracy = kCLLocationAccuracyBest;  
  9.   lm.distanceFilter = 1000.0f;  
  10.   [lm startUpdatingLocation];  
  11.   }  
  12.   }  
  13.   - (void) locationManager: (CLLocationManager *) manager  
  14.   didUpdateToLocation: (CLLocation *) newLocation  
  15.   fromLocation: (CLLocation *) oldLocation{  
  16.   NSString *lat = [[NSString alloc] initWithFormat:@"%g",  
  17.   newLocation.coordinate.latitude];  
  18.   latlatitudeTextField.text = lat;  
  19.   NSString *lng = [[NSString alloc] initWithFormat:@"%g",  
  20.   newLocation.coordinate.longitude];  
  21.   longitudeTextField.text = lng;  
  22.   NSString *acc = [[NSString alloc] initWithFormat:@"%g",  
  23.   newLocation.horizontalAccuracy];  
  24.   accaccuracyTextField.text = acc;  
  25.   [acc release];  
  26.   [lat release];  
  27.   [lng release]   
  28.   }  
  29.   - (void) locationManager: (CLLocationManager *) manager  
  30.   didFailWithError: (NSError *) error {  
  31.   NSString *msg = [[NSString alloc]  
  32.   initWithString:@"Error obtaining location"];  
  33.   UIAlertView *alert = [[UIAlertView alloc]  
  34.   initWithTitle:@"Error"  
  35.   message:msg  
  36.   delegate:nil  
  37.   cancelButtonTitle: @"Done"  
  38.   otherButtonTitles:nil];  
  39.   [alert show];  
  40.   [msg release];  
  41.   [alert release];  
  42.   }  
  43.   (void) dealloc{  
  44.  [lm release];  
  45.   [latitudeTextField release];  
  46.   [longitudeTextField release];  
  47.   [accuracyTextField release];  
  48.   [super dealloc];  
  49.   } 

前面的代碼創(chuàng)建了 CLLocationManager類的一個實例,在使用對象之前,你應(yīng)該檢查用戶是否開啟了設(shè)備的定位服務(wù),如果開啟了,你可以使用 desiredAccuracy屬性指定想要的精度,使用下面的常量指定想要的精度:

  1.  kCLLocationAccuracyBest  
  2. kCLLocationAccuracyNearestTenMeters  
  3.  kCLLocationAccuracyHundredMeters  
  4.  kCLLocationAccuracyKilometer  
  5. kCLLocationAccuracyThreeKilometers 

distanceFilter屬性讓你指定設(shè)備必須移動多少距離位置信息才會更新,這個屬性的單位是米。如果你想得到所有移動的通知,可以使用kCLDistanceFilterNone常量,最后,使用 startUpdatingLocation方法啟動位置管理器。

要獲得位置信息,需處理下面兩個事件:

  1. locationManager:didUpdateToLocation:fromLocation:  
  2. locationManager:didFailWithError: 

當獲得一個新的定位值時,設(shè)備觸發(fā) locationManager:didUpdateToLocation:fromLocation:事件,如果位置管理器不能確定位置信息,就會觸發(fā) locationManager:didFailWithError:事件。

當設(shè)備可以確定位置時,你可能想顯示經(jīng)緯度值和精度,這時你可以使用CLLocation對象,它的horizontalAccuracy屬性可以指定精度范圍,單位是米。

按Command- r在iPhone模擬器上測試該程序,圖2顯示了模擬器顯示的位置經(jīng)緯度值,同時顯示了精度。

iPhone GPS定位系統(tǒng) 實例操作

圖 2 定位測試:當你在iPhone模擬器上測試該示例程序時,總會顯示這些固定的值

顯示地圖

如果能將位置坐標定位到地圖上顯示將會更有趣,幸運的是,iPhone 3.0 SDK包括了Map Kit API,它可以讓你在程序中顯示Google Map,下面以一個例子進行說明。

還是使用前面創(chuàng)建的項目,在 LBSViewController.xib文件中視圖窗口上增加一個按鈕,如圖3所示。

iPhone GPS定位系統(tǒng) 實例操作

圖 3 View Map按鈕:增加按鈕后的樣子
  
在Xcode中框架組上點擊右鍵,增加一個新的框架MapKit.framework。在 LBSViewController.h文件中添加下列代碼中的粗體部分:

  1.   #import   
  2.   #import   
  3.   #import   
  4.   @interface LBSViewController : UIViewController  
  5.    {  
  6.   IBOutlet UITextField *accuracyTextField;  
  7.   IBOutlet UITextField *latitudeTextField;  
  8.   IBOutlet UITextField *longitudeTextField;  
  9.   CLLocationManager *lm;  
  10.   MKMapView *mapView;  
  11.   }  
  12.   @property (retain, nonatomic) UITextField *accuracyTextField;  
  13.   @property (retain, nonatomic) UITextField *latitudeTextField;  
  14.   @property (retain, nonatomic) UITextField *longitudeTextField;  
  15.   -(IBAction) btnViewMap: (id) sender;  
  16.   @end 

回到界面編輯器,拖動按鈕到文件的所有者項目上,然后選擇btnViewMap:。

在LBSViewController.m文件中,添加下列代碼中的粗體部分:

  1.   -(IBAction) btnViewMap: (id) sender {  
  2.   [self.view addSubview:mapView];  
  3.   }  
  4.   (void) viewDidLoad {  
  5.   lm = [[CLLocationManager alloc] init];  
  6.   lm.delegate = self;  
  7.   lm.desiredAccuracy = kCLLocationAccuracyBest;  
  8.     
  9.   lm.distanceFilter = 1000.0f;  
  10.   [lm startUpdatingLocation];  
  11.   mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];  
  12.   mapView.mapType = MKMapTypeHybrid;  
  13.   }  
  14.   - (void) locationManager: (CLLocationManager *) manager  
  15.   didUpdateToLocation: (CLLocation *) newLocation  
  16.   fromLocation: (CLLocation *) oldLocation{  
  17.   NSString *lat = [[NSString alloc] initWithFormat:@"%g",  
  18.   newLocation.coordinate.latitude];  
  19.   latlatitudeTextField.text = lat;  
  20.   NSString *lng = [[NSString alloc] initWithFormat:@"%g",  
  21.   newLocation.coordinate.longitude];  
  22.   longitudeTextField.text = lng;  
  23.   NSString *acc = [[NSString alloc] initWithFormat:@"%g",  
  24.   newLocation.horizontalAccuracy];  
  25.   accaccuracyTextField.text = acc;  
  26.   [acc release];  
  27.   [lat release];  
  28.   [lng release];  
  29.   MKCoordinateSpan span;  
  30.   span.latitudeDelta=.005;  
  31.   span.longitudeDelta=.005;  
  32.   MKCoordinateRegion region;  
  33.   region.center = newLocation.coordinate;  
  34.   region.span=span;  
  35.   [mapView setRegion:region animated:TRUE];  
  36.   }  
  37.   - (void) dealloc{  
  38.   [mapView release];  
  39.   [lm release];  
  40.   [latitudeTextField release];  
  41.   [longitudeTextField release];  
  42.   [accuracyTextField release];  
  43.   [super dealloc];  
  44.   } 

代碼解釋:

當視圖載入時創(chuàng)建一個MKMapView類的實例,設(shè)置顯示的地圖類型。

當用戶點擊View Map按鈕時,在當前視圖上增加mapView對象。

當位置信息得到更新時,使用mapView對象的setRegion:方法放大地圖。

在iPhone模擬器中按Command-r測試該程序,點擊View Map按鈕將會顯示一個包含位置管理器返回位置的地圖。如圖4所示。

iPhone GPS定位系統(tǒng) 實例操作

圖 4 地圖位置:通過定位內(nèi)核框架顯示地圖位置

因為模擬器始終顯示的是相同的位置,如果你有一部iPhone手機也可以真實地感受一下,當你移動位置時,你會看到地圖會自動更新。將 distanceFilter屬性設(shè)置得小一點,這樣可以增強跟蹤體驗。

正如你所看到的,iPhone SDK的定位內(nèi)核框架讓你可以很容易實現(xiàn)基于位置的設(shè)備,此外,MapKit(包括在iPhone SDK中)可以在地圖上顯示位置信息。如果沒有創(chuàng)建過基于位置的應(yīng)用,我想你看完本文就應(yīng)該動手一試,還等什么呢? via:http://ming-fanglin.javaeye.com/blog/703744

小結(jié):iPhone GPS定位系統(tǒng) 實例操作的內(nèi)容介紹完了,希望本文對你有所幫助!更多相關(guān)內(nèi)容請參考編輯推薦!

本篇文章來源于 黑軟基地-中國最大的黑客軟件安全教程下載站?。ㄊ謾C資訊) 原文鏈接:http://www.hackvip.com/mobiwen/html/Mobile_220397_3.html

責任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2014-11-13 14:07:04

2011-08-08 15:56:18

iPhone 震動 NSUserDefa

2011-07-07 16:42:38

iPhone Sqlite3 數(shù)據(jù)庫

2011-07-06 16:25:10

iPhone 程序 調(diào)用

2021-01-01 19:02:08

GPS定位欺騙網(wǎng)絡(luò)安全

2011-07-19 10:42:41

iPhone 應(yīng)用程序 模型

2011-07-19 11:12:07

iPhone 控制器

2011-05-27 15:56:30

Android

2011-07-26 18:11:56

iPhone Sqlite 數(shù)據(jù)庫

2011-08-03 16:01:24

iPhone應(yīng)用開發(fā) 自動登陸

2011-07-19 10:56:15

iPhone 控制器 視圖

2011-05-24 09:23:00

中電信定位云

2011-07-26 11:13:15

iPhone PXL

2011-07-06 16:55:56

iPhone php Push

2011-08-10 11:12:33

iPhone文件

2011-07-25 18:02:51

iPhone LibFetion 移植

2011-05-23 10:06:15

2011-07-18 17:52:47

Linux iPhone

2010-04-20 17:07:57

2009-06-20 13:37:18

iPhone OS 3iPhone OS
點贊
收藏

51CTO技術(shù)棧公眾號