iOS Location Service
来源:互联网 发布:javascript dom是什么 编辑:程序博客网 时间:2024/05/05 21:36
@implementation IFLocationManager@synthesize locationServiceFinished = _locationServiceFinished;@synthesize locationServiceFailed = _locationServiceFailed;- (id)init{ self = [super init]; if (self) { } return self;}#pragma mark -#pragma mark - 单例方法+ (instancetype)manager{ static IFLocationManager *core = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ core = [IFLocationManager new]; }); return core;}#pragma mark -#pragma mark - 启动定位服务- (void)startLocation{ if (locationManager) { locationManager.delegate = nil; locationManager = nil; } if ([CLLocationManager locationServicesEnabled]) { locationManager = [[CLLocationManager alloc] init]; locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; //不需要太高精度,费电 locationManager.delegate = self; [locationManager startUpdatingLocation]; }}#pragma mark -#pragma mark - 停止定位服务- (void)stopLocation{ [locationManager stopUpdatingLocation]; locationManager = nil;}#pragma mark -#pragma mark - 定位服务 - 更新地理位置回调方法- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation * newLocation = [locations lastObject];//最后一次位置是当前位置 CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init]; CLGeocodeCompletionHandler handle = ^(NSArray *placemarks,NSError *error) { for (CLPlacemark *placeMark in placemarks) { NSDictionary *addressDic = placeMark.addressDictionary; __unused NSString *state = [addressDic objectForKey:@"State"]; __unused NSString *city = [addressDic objectForKey:@"City"]; __unused NSString *subLocality = [addressDic objectForKey:@"SubLocality"]; __unused NSString *street = [addressDic objectForKey:@"Street"]; __unused NSString *latitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude]; __unused NSString *longitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude]; if (_locationServiceFinished) { NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys: state,LocationValue_State, city,LocationValue_City, subLocality,LocationValue_SubLocality, street,LocationValue_Street, latitude,LocationValue_Latitude, longitude,LocationValue_Longitude, placeMark.administrativeArea,LocationValue_AdministrativeArea, nil]; _locationServiceFinished(values); UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"12345" message:@"s" delegate:nil cancelButtonTitle:@"c" otherButtonTitles:nil, nil]; [a show]; } [self stopLocation]; } }; [clGeoCoder reverseGeocodeLocation:newLocation completionHandler:handle];}#pragma mark -#pragma mark - 定位服务 - 位置授权状态变化回调方法- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { switch (status) { case kCLAuthorizationStatusNotDetermined : if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { //iOS8新增 需要在info.plist中添加字段NSLocationAlwaysUsageDescription,值为空即可 [locationManager requestAlwaysAuthorization]; } if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { //iOS8新增 需要在info.plist中添加字段NSLocationWhenInUseUsageDescription,值为空即可 [locationManager requestWhenInUseAuthorization]; } } break; case kCLAuthorizationStatusRestricted: NSLog(@"kCLAuthorizationStatusRestricted"); break; case kCLAuthorizationStatusDenied : NSLog(@"kCLAuthorizationStatusDenied"); break; case kCLAuthorizationStatusAuthorizedAlways: case kCLAuthorizationStatusAuthorizedWhenInUse: break; default: break; }}#pragma mark -#pragma mark - 定位服务 - 获取地理位置数据失败回调方法- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { if (error) { if (error.code==1){ //不允许定位 // 定位服务没有得到您的授权,默认定位北京! if(_locationServiceFailed){ _locationServiceFailed(LocationServiceError_NOAuthorize); } }else{ //定位失败,默认定位北京! if(_locationServiceFailed){ _locationServiceFailed(LocationServiceError_LocationError); } } } [self stopLocation]; }@end
0 0
- iOS Location Service
- 地理位置服务(Location Service)
- 【android】Location-based Service
- LBS(Location Based Service)
- Mozilla Location Service-6
- Location Service 分析
- Moziila Location Service-7
- Mozilla Location Service-8
- Mozilla Location Service-9
- Mozilla Location Service-10
- 14-service之$location
- Service Location Protocol SLP
- [iOS开发站在巨人肩膀上]之 iOS 4 in Action - Using Location Monitoring Service
- ASP.NET MVC3 Service Location
- IOS Core Location
- ios Location 获取坐标
- iOS Location 定位功能
- ios-Core Location框架
- 托福阅读中的五种复习方法【zhasite】
- malloc.c
- Feature分支
- 异步调用与多线程的区别
- 远程连接不上vmware
- iOS Location Service
- Struts2 过滤 xss攻击 的一种解决方案
- 多人协作
- android Java String API
- FMDB 数据库简单用法
- arean.c
- jQuery入门学习笔记----第一章 jQuery开发入门
- 【bzoj4145】[AMPPZ2014]The Prices
- Javascript跨域和Ajax跨域解决方案