IOS 定位服务与地图的应用开发

时间:2023-03-08 20:03:24
IOS 定位服务与地图的应用开发

1.定位服务

现在的移动设备很多都提供定位服务,IOS设备提供3种不同定位途径:

(1)WiFi定位,通过查询一个WiFi路由器的地理位置的信息,比较省电;IPhone,IPod touch和IPad都可以采用。

(2)蜂窝式移动电话基站定位,通过移动运营商基站定位,只有Iphone,3G版本的IPod touch和Ipad可以采用

(3)GPS卫星定位,通过3~4颗GPS卫星定位,最为准确,但是耗电量大,不能遮挡,IPhone,IPod touch和IPad都可以采用

IOS不像Android系统在定位服务编程时可以指定采用哪种途径进行定位。IOS的API把底层这些细节屏蔽掉了,开发人员和用户并不知道现在设备采用哪种方式进行定位,IOS系统会根据设备的情况和周围的环境,采用一套最佳的解决方案。这个方案是这样的:如果能够接收GPS信息,那么设备优先采用GPS定位,否者采用WiFi或者蜂窝基站定位,在WiFi和蜂窝基站之间优先选择使用WiFi,如果无法连接WiFi才使用蜂窝基站定位。

1.1 定位服务编程

定位在IOS6之后,API没有太大的变化,它主要使用CoreLocation框架,定位时主要使用CLLocationManamger , CLLocationManagerDelegate和CLLocation. CLLocationManager是定位服务管理类,它能够使我们获得设备的位置信息和高度信息,也可以监控设备进入某个区域,他还可以帮组获得设备的运行方向等。CLLocationManagerDelegate是CLLocationManager类委托协议。CLLocation封装了位置和高度信息。

下面通过一个实例介绍一下使用定位服务编程,在应用启动时,进入画面会或得位置信息,并显示在对应的文本框中,如果设备位置发生变化,也会重新或得位置信息,并更新对应的文本框。

首先需要添加CorLocation.framework框架。在ViewController.h中代码如下:

 #import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
#import <AddressBook/AddressBook.h> @interface HBViewController : UIViewController<CLLocationManagerDelegate>
//经度
@property (weak, nonatomic) IBOutlet UILabel *lbLng;
//纬度
@property (weak, nonatomic) IBOutlet UILabel *lbLat;
//高度
@property (weak, nonatomic) IBOutlet UILabel *lbAlt;
//城市地址
@property (weak, nonatomic) IBOutlet UILabel *lbAddress; @property (nonatomic,strong)CLLocationManager *locationManager;
@property (nonatomic,strong)CLLocation *currLocation; - (IBAction)goToNextPage:(id)sender; - (IBAction)reverseGeocode:(id)sender;
@end

在h文件中,首先引入<CorLocation/CorLocation.h>和<CoreLocation/CLLocationManagerDelegate.h>头文件,然后再定义ViewController时,需要声明实现CLLocationManagerDelegate协议

ViewController.m的viewDidLoad代码如下:

 - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. //定位服务管理对象初始化
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
//(desired 期望)(Accuracy准确度)
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 100.0f;
}

在viewDidLoad方法中主要对CLLoacationManager的成员变量_locationManager进行初始化。并且给_locationManager设置了desiredAccuracy属性,desiredAccuracy属性是一个非常重要的属性,他的取值有6个常量:

(1)KCLLocationAccuracyNearestTenMeters 精度10米

(2)KCLLocationAccuracyHundredMeters 精度100米

(3)KCLLocationAccuracyKilometer 精度1000米

(4)KCLLocationAccuracyThreeKilometers 精度3000米

(5)KCLLocationAccuracyBest 设备使用电池供电时候,最高的精度

(6)kCLLocationAccuratyBestForNavigation 导航情况下最高精度,一般要有外接电源时才能使用

精度越高请求的位置信息的频率就高,这就意味着设备越耗电

最后设置的distanceFilter属性,distanceFilter属性时距离过滤器,它定义了设备移动更新位置的最小距离,他的单位是米

初始化CLLocationManager完成之后,需要使用startUpdatingLocation方法开始定位服务,他是在ViewController.m的viewWillAppear:方法中,代码如下:

 -(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]; //开始定位
[_locationManager startUpdatingLocation];
}

在离开页面时,关闭定位服务:

 -(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated]; //停止定位
[_locationManager stopUpdatingLocation];
}

viewWillDisappear:在视图消失时调用,能够保证及时地关闭定位服务,这是负责人的做法。在IOS6之后,请求有所变化,地位服务应用退到后台后可以延迟更新位置信息,其中allowDeferredLocationUpdatesUntilTraveld:timeout方法可以设置延迟更新,从而使得应用在后台不再更新位置信息,关闭延迟更新使用disallowDeferredLocationUpdates方法实现。此外,在IOS6之后新增pausesLocationUpdatesAutomatically属性,他能设定自动暂停位置更新,定位服务的开启和暂停管理权交给系统,这样会更加合理和简单。

一旦定位服务开启,并设置好了CLLocationManager委托属性delegate后,当用户设备移动到达过滤距离时,就会回调委托方法,与定位服务有关的方法有两个。代码如下:

 #pragma mark Core Loacation委托方法实现位置的更新
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
self.currLocation = [locations lastObject];
_lbLat.text = [NSString stringWithFormat:@"%3.5f",self.currLocation.coordinate.latitude];
_lbLng.text = [NSString stringWithFormat:@"%3.5f",self.currLocation.coordinate.longitude];
_lbAlt.text = [NSString stringWithFormat:@"%3.5f",self.currLocation.altitude];
} -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"error:%@",error);
}

其中latitude为纬度信息,longitude为经度信息

1.2 地理信息反编码

我们可以通过精度纬度来获取到给定点的地理坐标,返回这个地点的相关文字描述信息,这些文字描述信息被封装在CLPlaceMark类中,我们把这个类叫做“地标”类,地标类有很多属性,下面是主要的几个文字描述相关属性:

(1)addressDictionary,地址信息的字典,包含一些键值对,其中的键是在AddressBook.framework中定义好的;

(2)IOScountryCode,IOS国家代号

(3)country,国家信息

(4)postalCode邮政编码

(5)administrativeArea 行政区域信息

(6)subAdministrativeArea 行政区域附加信息

(7)locality 指定城市信息

(8)subLocality 指定城市信息附加信息

(9)thoroughfare 指定街道级别信息

(10)subThoroughfare指定街道级别的附加信息

地理信息反编码使用CLGeocoder类实现,这个类能够实现在地理坐标与地理文字描述信息之间的转换。CLGeocoder类中进行地理信息反编码的方法是:

-(void)reverseGeocoderLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler

其中,参数location时要定位的地理位置对象,completionHandler参数指定了一个代码块CLGeocoderCompletionHandler对象,用于地理信息反编码之后的回调。在试图控制器中添加按钮,设计其点击时间,代码如下:

 - (IBAction)reverseGeocode:(id)sender {

     CLGeocoder *geocoder = [[CLGeocoder alloc]init];

     [geocoder reverseGeocodeLocation:self.currLocation completionHandler:^(NSArray *placemarks, NSError *error) {

         if([placemarks count]>)
{
CLPlacemark *placemark = placemarks[];
NSDictionary *addressDictionary = placemark.addressDictionary; NSString *address = [addressDictionary objectForKey:(NSString *)kABPersonAddressStreetKey];
address=address==nil?@"":address; NSString *state=[addressDictionary objectForKey:(NSString *)kABPersonAddressStateKey];
state=state==nil?@"":state; NSString *city = [addressDictionary objectForKey:(NSString *)kABPersonAddressCityKey];
city=city==nil?@"":city; NSString *addressName=[addressDictionary objectForKey:@"Name"]; //_lbAddress.text=[NSString stringWithFormat:@"%@%@%@",state,city,address];
_lbAddress.text=[NSString stringWithFormat:@"%@",addressName];
}
}];
}

注:需要在头文件中引入代码#import <AddressBook/AddressBook.h>

1.3 地理信息编码查询

地理信息编码查询与反编码刚好相反,他给定地理信息的文件描述,查询出来相关的地理坐标,这种查询结果也是一个集合。地理信息编码查询也是采用CLGeocoder类,其中有关地理信息编码的方法有:

(1)geocodeAddressDictionary:completionHandler: 通过指定一个地址信息字典对象参数进行查询。

(2)geocodeAddressString:completionHandler: 通过指定一个地址字符串参数进行查询。

(3)geocodeAddressString:inRegion: completionHandler 通过指定地址字符串和查询的范围作为参数进行查询,其中inRegion部分时指定查询范围,他是CLRegion类型。在UIViewController中按钮的代码如下:

 -(IBAction)geocodeQuery:(id)sender
{
if(_lbAddress.text == nil || [_lbAddress.text length] == )
{
return;
} CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder geocodeAddressString:_lbAddress.text completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"查询记录数:%i",[placemarks count]); if([placemarks count]>)
{
CLPlacemark *placemark = [placemarks objectAtIndex:];
CLLocationCoordinate2D coordinate = placemark.location.coordinate;
float latitude=coordinate.latitude;
float longitude= coordinate.longitude;
NSString *strCoordinate = [NSString stringWithFormat:@"经度:%3.5f\n 纬度:%3.5f",latitude,longitude]; NSDictionary *addressDictionary = placemark.addressDictionary; NSString *address = [addressDictionary objectForKey:(NSString *)kABPersonAddressStreetKey];
address = address ==nil?@"":address; NSString *state = [addressDictionary objectForKey:(NSString *)kABPersonAddressStateKey];
state = state==nil?@"":state; NSString *city = [addressDictionary objectForKey:(NSString *)kABPersonAddressCityKey];
city = city ==nil?@"":city; [_lbAddress resignFirstResponder];
}
}];
}

运行显示效果如下:IOS 定位服务与地图的应用开发