实战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文件中添加以下粗体字显示的代码:

Java代码
  1. #import <UIKit/UIKit.h>  
  2. #import <CoreLocation/CoreLocation.h>  
  3. @interface LBSViewController : UIViewController  
  4.     <CLLocationManagerDelegate> {  
  5.     IBOutlet UITextField *latitudeTextField;  
  6.     IBOutlet UITextField *longitudeTextField;  
  7.     IBOutlet UITextField *accuracyTextField;  
  8.     CLLocationManager *lm;  
  9. }  
  10. @property (retain, nonatomic) UITextField *latitudeTextField;  
  11. @property (retain, nonatomic) UITextField *longitudeTextField;  
  12. @property (retain, nonatomic) UITextField *accuracyTextField;  
  13. @end  


  若要使用CLLocationManager类,需要在你的视图控制器类中实现CLLocationManagerDelegate协议,还需要创建三个出口用于连接视图窗口中的三个TextFiled视图。

 

  回到界面编辑器,单击并拖动文档的所有者项目到三个TextField视图,然后分别选择latitudeTextField,longitudeTextField和accuracyTextField。

  在 LBSViewController.m文件中查询以下代码中的粗体部分:

Java代码
  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.   
  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.     latitudeTextField.text = lat;  
  20.       
  21.     NSString *lng = [[NSString alloc] initWithFormat:@"%g",  
  22.         newLocation.coordinate.longitude];  
  23.     longitudeTextField.text = lng;  
  24.       
  25.     NSString *acc = [[NSString alloc] initWithFormat:@"%g",  
  26.         newLocation.horizontalAccuracy];  
  27.     accuracyTextField.text = acc;      
  28.       
  29.     [acc release];  
  30.     [lat release];  
  31.     [lng release];  
  32. }  
  33. - (void) locationManager: (CLLocationManager *) manager  
  34.     didFailWithError: (NSError *) error {  
  35.     NSString *msg = [[NSString alloc]  
  36.        initWithString:@"Error obtaining location"];  
  37.     UIAlertView *alert = [[UIAlertView alloc]  
  38.                           initWithTitle:@"Error"  
  39.                           message:msg  
  40.                           delegate:nil  
  41.                           cancelButtonTitle: @"Done"  
  42.                           otherButtonTitles:nil];  
  43.     [alert show];      
  44.     [msg release];  
  45.     [alert release];  
  46. }  
  47. - (void) dealloc{  
  48.     [lm release];  
  49.     [latitudeTextField release];  
  50.     [longitudeTextField release];  
  51.     [accuracyTextField release];  
  52.     [super dealloc];  
  53. }  


  前面的代码创建了 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文件中添加下列代码中的粗体部分:

 

Java代码
  1. #import <UIKit/UIKit.h>  
  2. #import <CoreLocation/CoreLocation.h>  
  3. #import <MapKit/MapKit.h>  
  4. @interface LBSViewController : UIViewController  
  5.     <CLLocationManagerDelegate> {  
  6.     IBOutlet UITextField *accuracyTextField;  
  7.     IBOutlet UITextField *latitudeTextField;  
  8.     IBOutlet UITextField *longitudeTextField;  
  9.     CLLocationManager *lm;  
  10.     
  11.     MKMapView *mapView;  
  12. }  
  13. @property (retain, nonatomic) UITextField *accuracyTextField;  
  14. @property (retain, nonatomic) UITextField *latitudeTextField;  
  15. @property (retain, nonatomic) UITextField *longitudeTextField;  
  16.   
  17. -(IBAction) btnViewMap: (id) sender;  
  18.   
  19. @end  

 

  回到界面编辑器,拖动按钮到文件的所有者项目上,然后选择btnViewMap:。

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

 

Java代码
  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.     lm.distanceFilter = 1000.0f;  
  9.     [lm startUpdatingLocation];  
  10.       
  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.     latitudeTextField.text = lat;  
  20.       
  21.     NSString *lng = [[NSString alloc] initWithFormat:@"%g",  
  22.         newLocation.coordinate.longitude];  
  23.     longitudeTextField.text = lng;  
  24.       
  25.     NSString *acc = [[NSString alloc] initWithFormat:@"%g",  
  26.         newLocation.horizontalAccuracy];  
  27.     accuracyTextField.text = acc;      
  28.       
  29.     [acc release];  
  30.     [lat release];  
  31.     [lng release];  
  32.       
  33.     MKCoordinateSpan span;  
  34.     span.latitudeDelta=.005;  
  35.     span.longitudeDelta=.005;  
  36.       
  37.     MKCoordinateRegion region;  
  38.     region.center = newLocation.coordinate;  
  39.     region.span=span;  
  40.       
  41.     [mapView setRegion:region animated:TRUE];  
  42. }  
  43. - (void) dealloc{  
  44.     [mapView release];  
  45.     [lm release];  
  46.     [latitudeTextField release];  
  47.     [longitudeTextField release];  
  48.     [accuracyTextField release];  
  49.     [super dealloc];  
  50. }  


  代码解释:

 

  l 当视图载入时创建一个MKMapView类的实例,设置显示的地图类型。

  l 当用户点击View Map按钮时,在当前视图上增加mapView对象。

  l 当位置信息得到更新时,使用mapView对象的setRegion:方法放大地图。

  在iPhone模拟器中按Command-r测试该程序,点击View Map按钮将会显示一个包含位置管理器返回位置的地图。如图4所示。

   
  图 4 地图位置:通过定位内核框架显示地图位置

  因为模拟器始终显示的是相同的位置,如果你有一部iPhone手机也可以真实地感受一下,当你移动位置时,你会看到地图会自动更新。将 distanceFilter属性设置得小一点,这样可以增强跟踪体验。

http://ming-fanglin.javaeye.com/blog/703744

posted @ 2010-10-21 15:40  周宏伟  阅读(10780)  评论(5编辑  收藏  举报