iPhone GPS定位系统 实例操作是本文要介绍的内容,先来看本文详细内容。如今,配备GPS功能的移动设备越来越普遍了,使用GPS定位系统,可以精确地定位你当前所在的地理位置,但由于GPS接收机需要对准天空才能工作,因此在室内环境基本无用。
另一个找到自己所在位置的有效方法是使用手机基站,手机开机时,它会与周围的基站保持联系,如果你知道这些基站的身份,就可以使用各种数据库 (包含基站的身份和它们的确切地理位置)计算出手机的物理位置。基站不需要卫星,和GPS不同,它对室内环境一样管用。但它没有GPS那样精确,它的精度取决于基站的密度,它在基站密集型区域的准确度最高。
提示:第一代iPhone并没有配置GPS接收器,基站方式不能应用到iPod Touch上,因为它不是手机。
第三种方法是依赖Wi-Fi,使用这种方法时,设备连接到Wi-Fi网络,通过检查服务提供商的数据确定位置,它既不依赖卫星,也不依赖基站,因此这个方法对于可以连接到Wi-Fi网络的区域有效,但它的精确度也是这三个方法中最差的。
定位框架内核
在iPhone上,苹果提供了定位框架内核,以帮助你确定你的物理位置,这个框架的美妙之处在于它使用了前面提到的所有三种方法,具体使用的是哪种方法对于开发者来说是透明的,开发人员只需要指定所需要的精度,定位内核将会以最佳方式确定定位结果。
你一定感到很吃惊吧?!本文其余部分将向你展示这是如何做到的。
获取位置坐标
使用Xcode,创建一个新的基于视图的应用程序项目,取名为LBS,在新项目中,双击LBSViewController.xib文件,在界面设计工具中编辑它。使用下面的组件填充视图窗口,如图1所示。
Label
TextField
图 1 位置视图实例:用Label和TextFiled填充这个窗口
在Xcode中框架组上点击右键,选择“添加”*“现有框架”,选择 “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类,需要在你的视图控制器类中实现CLLocationManagerDelegate协议,还需要创建三个出口用于连接视图窗口中的三个TextFiled视图。
回到界面编辑器,单击并拖动文档的所有者项目到三个TextField视图,然后分别选择latitudeTextField,longitudeTextField和accuracyTextField。
在 LBSViewController.m文件中查询以下代码中的粗体部分:
1. #import "LBSViewController.h"
2. @implementation LBSViewController
3. @synthesize latitudeTextField, longitudeTextField, accuracyTextField;
4. - (void) viewDidLoad {
5. lm
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
17. newLocation.coordinate.latitude];
18. latlatitudeTextField.text
19. NSString *lng
20. newLocation.coordinate.longitude];
21. longitudeTextField.text = lng;
22. NSString *acc
23. newLocation.horizontalAccuracy];
24. accaccuracyTextField.text
25. [acc release];
26. [lat release];
27. [lng release]
28. }
29. - (void) locationManager: (CLLocationManager *) manager
30. didFailWithError: (NSError *) error {
31. NSString *msg
32. initWithString:@"Error obtaining location"];
33. UIAlertView *alert
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. }
前面的代码创建了 CLLocationManager类的一个实例,在使用对象之前,你应该检查用户是否开启了设备的定位服务,如果开启了,你可以使用 desiredAccuracy属性指定想要的精度,使用下面的常量指定想要的精度:
1. kCLLocationAccuracyBest
2. kCLLocationAccuracyNearestTenMeters
3. kCLLocationAccuracyHundredMeters
4. kCLLocationAccuracyKilometer
5. kCLLocationAccuracyThreeKilometers
distanceFilter属性让你指定设备必须移动多少距离位置信息才会更新,这个属性的单位是米。如果你想得到所有移动的通知,可以使用kCLDistanceFilterNone常量,最后,使用 startUpdatingLocation方法启动位置管理器。
要获得位置信息,需处理下面两个事件:
1. locationManager:didUpdateToLocation:fromLocation:
2. 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文件中添加下列代码中的粗体部分:
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
6. lm.delegate = self;
7. lm.desiredAccuracy = kCLLocationAccuracyBest;
8.
9. lm.distanceFilter = 1000.0f;
10. [lm startUpdatingLocation];
11. mapView
12. mapView.mapType = MKMapTypeHybrid;
13. }
14. - (void) locationManager: (CLLocationManager *) manager
15. didUpdateToLocation: (CLLocation *) newLocation
16. fromLocation: (CLLocation *) oldLocation{
17. NSString *lat
18. newLocation.coordinate.latitude];
19. latlatitudeTextField.text
20. NSString *lng
21. newLocation.coordinate.longitude];
22. longitudeTextField.text = lng;
23. NSString *acc
24. newLocation.horizontalAccuracy];
25. accaccuracyTextField.text
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. }
代码解释:
当视图载入时创建一个MKMapView类的实例,设置显示的地图类型。
当用户点击View Map按钮时,在当前视图上增加mapView对象。
当位置信息得到更新时,使用mapView对象的setRegion:方法放大地图。
在iPhone模拟器中按Command-r测试该程序,点击View Map按钮将会显示一个包含位置管理器返回位置的地图。如图4所示。
图 4 地图位置:通过定位内核框架显示地图位置
因为模拟器始终显示的是相同的位置,如果你有一部iPhone手机也可以真实地感受一下,当你移动位置时,你会看到地图会自动更新。将 distanceFilter属性设置得小一点,这样可以增强跟踪体验。
正如你所看到的,iPhone SDK的定位内核框架让你可以很容易实现基于位置的设备,此外,MapKit(包括在iPhone SDK中)可以在地图上显示位置信息。如果没有创建过基于位置的应用,我想你看完本文就应该动手一试,还等什么呢? via:http://ming-fanglin.javaeye.com/blog/703744