iPhone應用開發之:GPS定位系統
如今,配備GPS功能的行動裝置越來越普遍了,使用GPS定位系統,可以精確地定位你當前所在的地理位置,但由於GPS接收機需要對準天空才能工作,因此在室內環境基本無用。
另一個找到自己所在位置的有效方法是使用手機基站,手機開機時,它會與周圍的基站保持聯絡,如果你知道這些基站的身份,就可以使用各種資料庫(包含基站的身份和它們的確切地理位置)計算出手機的物理位置。基站不需要衛星,和GPS不同,它對室內環境一樣管用。但它沒有GPS那樣精確,它的精度取決於基站的密度,它在基站密集型地區的準確度最高。
提示:第一代iPhone並沒有配置GPS接收器,基站方式不能應用到iPod Touch上,因為它不是手機。
第三種方法是依賴Wi-Fi,使用這種方法時,裝置串連到Wi-Fi網路,通過檢查服務提供者的資料確定位置,它既不依賴衛星,也不依賴基站,因此這個方法對於可以串連到Wi-Fi網路的地區有效,但它的精確度也是這三個方法中最差的。
定位架構核心
在iPhone上,蘋果提供了定位架構核心,以協助你確定你的物理位置,這個架構的美妙之處在於它使用了前面提到的所有三種方法,具體使用的是哪種方法對於開發人員來說是透明的,開發人員只需要指定所需要的精度,定位核心將會以最佳方式確定定位結果。
你一定感到很吃驚吧?!本文其餘部分將向你展示這是如何做到的。
擷取位置座標
使用Xcode,建立一個新的基於視圖的應用程式項目,取名為LBS,在新項目中,雙擊LBSViewController.xib檔案,在介面設計工具中編輯它。使用下面的組件填充視圖視窗,1所示。
l Label
l TextField
圖 1 位置視圖執行個體:用Label和TextFiled填充這個視窗
在Xcode中架構組上點擊右鍵,選擇“添加”*“現有架構”,選擇“Framework/CoreLocation.framework”,向LBSViewController.h檔案中添加以下粗體字顯示的代碼:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface LBSViewController : UIViewController
<CLLocationManagerDelegate> {
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類,需要在你的視圖控制器類中實現CLLocationManagerDelegate協議,還需要建立三個出口用於串連視圖視窗中的三個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];
latitudeTextField.text = lat;
NSString *lng = [[NSString alloc] initWithFormat:@"%g",
newLocation.coordinate.longitude];
longitudeTextField.text = lng;
NSString *acc = [[NSString alloc] initWithFormat:@"%g",
newLocation.horizontalAccuracy];
accuracyTextField.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];
} 前面的代碼建立了CLLocationManager類的一個執行個體,在使用對象之前,你應該檢查使用者是否開啟了裝置的定位服務,如果開啟了,你可以使用desiredAccuracy屬性指定想要的精度,使用下面的常量指定想要的精度:
l kCLLocationAccuracyBest
l kCLLocationAccuracyNearestTenMeters
l kCLLocationAccuracyHundredMeters
l kCLLocationAccuracyKilometer
l kCLLocationAccuracyThreeKilometers
distanceFilter屬性讓你指定裝置必須移動多少距離位置資訊才會更新,這個屬性的單位是米。如果你想得到所有移動的通知,可以使用kCLDistanceFilterNone常量,最後,使用startUpdatingLocation方法啟動位置管理器。
要獲得位置資訊,需處理下面兩個事件:
l locationManager:didUpdateToLocation:fromLocation:
l locationManager:didFailWithError:
當獲得一個新的定位值時,裝置觸發locationManager:didUpdateToLocation:fromLocation:事件,如果位置管理器不能確定位置資訊,就會觸發locationManager:didFailWithError:事件。
當裝置可以確定位置時,你可能想顯示經緯度值和精度,這時你可以使用CLLocation對象,它的horizontalAccuracy屬性可以指定精度範圍,單位是米。
按Command-r在iPhone模擬器上測試該程式,圖2顯示了模擬器顯示的位置經緯度值,同時顯示了精度。
圖 2 定位測試:當你在iPhone模擬器上測試該樣本程式時,總會顯示這些固定的值
顯示地圖
如果能將位置座標定位到地圖上顯示將會更有趣,幸運的是,iPhone 3.0 SDK包括了Map Kit API,它可以讓你在程式中顯示Google Map,下面以一個例子進行說明。
還是使用前面建立的項目,在LBSViewController.xib檔案中視圖視窗上增加一個按鈕,3所示。
圖 3 View Map按鈕:增加按鈕後的樣子
在Xcode中架構組上點擊右鍵,增加一個新的架構MapKit.framework。在LBSViewController.h檔案中添加下列代碼中的粗體部分:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface LBSViewController : UIViewController
<CLLocationManagerDelegate> {
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];
latitudeTextField.text = lat;
NSString *lng = [[NSString alloc] initWithFormat:@"%g",
newLocation.coordinate.longitude];
longitudeTextField.text = lng;
NSString *acc = [[NSString alloc] initWithFormat:@"%g",
newLocation.horizontalAccuracy];
accuracyTextField.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];
}
代碼解釋:
l 當視圖載入時建立一個MKMapView類的執行個體,設定顯示的地圖類型。
l 當使用者點擊View Map按鈕時,在當前視圖上增加mapView對象。
l 當位置資訊得到更新時,使用mapView對象的setRegion:方法放大地圖。
在iPhone模擬器中按Command-r測試該程式,點擊View Map按鈕將會顯示一個包含位置管理器返回位置的地圖。4所示。
圖 4 地圖位置:通過定位核心架構顯示地圖位置
因為模擬器始終顯示的是相同的位置,如果你有一部iPhone手機也可以真實地感受一下,當你移動位置時,你會看到地圖會自動更新。將distanceFilter屬性設定得小一點,這樣可以增強跟蹤體驗。
正如你所看到的,iPhone SDK的定位核心架構讓你可以很容易實現基於位置的裝置,此外,MapKit(包括在iPhone SDK中)可以在地圖上顯示位置資訊。如果沒有建立過基於位置的應用,我想你看完本文就應該動手一試,還等什麼呢?
以上內容轉載自:http://www.3gtarena.com/News_View.asp?NewsID=1270