iOS—使用CoreLocation框架获取定位信息
2014-05-13 17:14
597 查看
iOS自带的CoreLocation框架使用Delegate将定位封装得很好用,下面我采用单例设计模式创建一个定位管理类:
调用的代码如下:
// // MyLocationManager.h // Common // // Created by victor on 14-5-13. // Copyright (c) 2014年 visp. All rights reserved. // #import <Foundation/Foundation.h> #import <CoreLocation/CoreLocation.h> typedef void (^CLLocationFailHandler)(NSError *error); @interface MyLocationManager : NSObject <CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *myLocationManager; @property (nonatomic, strong) CLGeocoder *myGeocoder; @property (nonatomic, assign) CLGeocodeCompletionHandler completionHandler; @property (nonatomic, assign) CLLocationFailHandler failHandler; + (id)defaultLocation; - (void)startLocationManagerWithSuccess:(CLGeocodeCompletionHandler)completionHandler error:(CLLocationFailHandler)failHandler; @end
// // MyLocationManager.m // Common // // Created by victor on 14-5-13. // Copyright (c) 2014年 visp. All rights reserved. // #import "MyLocationManager.h" static id locationManager; @implementation MyLocationManager + (id)defaultLocation { if (locationManager == nil) { locationManager = [[self alloc] init]; } return locationManager; } - (void)startLocationManagerWithSuccess:(CLGeocodeCompletionHandler)completionHandler error:(CLLocationFailHandler)failHandler { if ([CLLocationManager locationServicesEnabled]) { self.completionHandler = completionHandler; self.failHandler = failHandler; self.myLocationManager = [[CLLocationManager alloc] init]; self.myLocationManager.delegate = self; [self.myLocationManager startUpdatingLocation]; } else { NSLog(@"Location services are not enabled"); } } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *location = [locations objectAtIndex:0]; NSLog(@"Location is completed"); NSLog(@"Latitude = %f", location.coordinate.latitude); NSLog(@"Longitude = %f", location.coordinate.longitude); [self.myLocationManager stopUpdatingLocation]; self.myGeocoder = [[CLGeocoder alloc] init]; NSLog(@"Start reverse geocode location"); [self.myGeocoder reverseGeocodeLocation:location completionHandler:self.completionHandler]; } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { self.failHandler(error); } @end
调用的代码如下:
- (void)configureLocationManager { [[MyLocationManager defaultLocation] startLocationManagerWithSuccess:^(NSArray *placemarks, NSError *error) { if (error == nil && [placemarks count] > 0) { CLPlacemark *placemark = [placemarks objectAtIndex:0]; NSDictionary *address = placemark.addressDictionary; NSLog(@"Country = %@", placemark.country); NSLog(@"Postal Code = %@", placemark.postalCode); NSLog(@"Locality = %@", placemark.locality); NSString *state=[address objectForKey:@"State"]; NSString *city=[address objectForKey:@"City"]; NSString *subLocality=[address objectForKey:@"SubLocality"]; NSString *street=[address objectForKey:@"Street"]; NSLog(@"Location = %@", [NSString stringWithFormat:@"%@%@%@%@",state, city, subLocality, street]); } else if (error == nil && [placemarks count] == 0) { NSLog(@"No results were returned"); } else if (error != nil) { NSLog(@"An error occurred = %@", error); } } error:^(NSError *error) { NSLog(@"%@", [error localizedDescription]); }]; }
相关文章推荐
- iOS CoreLocation框架第三章—— CLGeocoder(地理编码器)和CLPlacemark(获取位置信息)
- IOS CoreLocation框架的使用(用于地理定位)
- ios-使用Core Location框架定位
- iOS开发之CoreLocation框架(地图/定位)
- IOS学习之Core Location定位和MapKit视图使用
- 【iOS】7.4 定位服务->2.1.3.2 定位 - 官方框架CoreLocation 功能2:地理编码和反地理编码
- IOS中使用百度地图定位后获取城市坐标,城市名称,城市编号信息
- 【iOS】7.4 定位服务->2.1.1 定位 - 官方框架CoreLocation: 请求用户授权
- 【iOS】7.4 定位服务->2.1.2 定位 - 官方框架CoreLocation: CLLocationManager(位置管理器)
- 【iOS】7.4 定位服务->2.1.3.3 定位 - 官方框架CoreLocation 功能3:区域监听
- 【iOS】7.4 定位服务->2.1.3.1 定位 - 官方框架CoreLocation 功能1:地理定位
- CoreLocation框架的使用---定位,求两地距离
- iOS定位-利用CoreLocation.framework获取当前城市
- ios 使用CoreLocation框架确定他的物理位置
- ios 利用自带的CoreLocation.framework 实现获取手机当前处于的精度和纬度 并计算两点之间的间距(特别使用于团购,附近商户之类的项目)
- 【iOS】7.4 定位服务->2.1.3.3 定位 - 官方框架CoreLocation 功能3:区域监听
- IOS学习之十二:Core Location定位和MapKit视图使用
- iOS - 使用CoreLocation获取当前所在城市
- iOS开发中使用CoreLocation框架处理地理编码的方法
- iOS中的定位-CoreLocation框架