iPhone GPS定位系統(tǒng) 實例操作
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
圖 1 位置視圖實例:用Label和TextFiled填充這個窗口
在Xcode中框架組上點擊右鍵,選擇“添加”*“現(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類,需要在你的視圖控制器類中實現(xiàn)CLLocationManagerDelegate協(xié)議,還需要創(chuàng)建三個出口用于連接視圖窗口中的三個TextFiled視圖。
回到界面編輯器,單擊并拖動文檔的所有者項目到三個TextField視圖,然后分別選擇latitudeTextField,longitudeTextField和accuracyTextField。
在 LBSViewController.m文件中查詢以下代碼中的粗體部分:
- #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類的一個實例,在使用對象之前,你應(yīng)該檢查用戶是否開啟了設(shè)備的定位服務(wù),如果開啟了,你可以使用 desiredAccuracy屬性指定想要的精度,使用下面的常量指定想要的精度:
- kCLLocationAccuracyBest
- kCLLocationAccuracyNearestTenMeters
- kCLLocationAccuracyHundredMeters
- kCLLocationAccuracyKilometer
- kCLLocationAccuracyThreeKilometers
distanceFilter屬性讓你指定設(shè)備必須移動多少距離位置信息才會更新,這個屬性的單位是米。如果你想得到所有移動的通知,可以使用kCLDistanceFilterNone常量,最后,使用 startUpdatingLocation方法啟動位置管理器。
要獲得位置信息,需處理下面兩個事件:
- locationManager:didUpdateToLocation:fromLocation:
- locationManager:didFailWithError:
當獲得一個新的定位值時,設(shè)備觸發(fā) locationManager:didUpdateToLocation:fromLocation:事件,如果位置管理器不能確定位置信息,就會觸發(fā) locationManager:didFailWithError:事件。
當設(shè)備可以確定位置時,你可能想顯示經(jīng)緯度值和精度,這時你可以使用CLLocation對象,它的horizontalAccuracy屬性可以指定精度范圍,單位是米。
按Command- r在iPhone模擬器上測試該程序,圖2顯示了模擬器顯示的位置經(jīng)緯度值,同時顯示了精度。
圖 2 定位測試:當你在iPhone模擬器上測試該示例程序時,總會顯示這些固定的值
顯示地圖
如果能將位置坐標定位到地圖上顯示將會更有趣,幸運的是,iPhone 3.0 SDK包括了Map Kit API,它可以讓你在程序中顯示Google Map,下面以一個例子進行說明。
還是使用前面創(chuàng)建的項目,在 LBSViewController.xib文件中視圖窗口上增加一個按鈕,如圖3所示。
圖 3 View Map按鈕:增加按鈕后的樣子
在Xcode中框架組上點擊右鍵,增加一個新的框架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
回到界面編輯器,拖動按鈕到文件的所有者項目上,然后選擇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];
- }
代碼解釋:
當視圖載入時創(chuàng)建一個MKMapView類的實例,設(shè)置顯示的地圖類型。
當用戶點擊View Map按鈕時,在當前視圖上增加mapView對象。
當位置信息得到更新時,使用mapView對象的setRegion:方法放大地圖。
在iPhone模擬器中按Command-r測試該程序,點擊View Map按鈕將會顯示一個包含位置管理器返回位置的地圖。如圖4所示。
圖 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