iOS学习系列——获取位置信息
2015-05-18 22:27
435 查看
1.加载动态库
定位和反查位置信息要加载两个动态库 CoreLocation.framework 和 MapKit.framework 一个获取坐标一个提供反查2. Info.plist文件添加两个数据
NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription3.代码(部分)
AppDelegate.h#import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> #import <MapKit/MKMapView.h> @interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate> { CLLocationManager *_locationManager; CLGeocoder *_geocoder; }
AppDelegate.m
const double a = 6378245.0; const double ee = 0.00669342162296594323; const double pi = 3.14159265358979324;
/** * 开启定位服务 */ - (void)startLocation { if ([CLLocationManager locationServicesEnabled]) { _locationManager = [[CLLocationManager alloc] init]; _geocoder = [[CLGeocoder alloc] init]; _locationManager.delegate = self; _locationManager.desiredAccuracy = kCLLocationAccuracyBest;//最精确的定位 _locationManager.distanceFilter = 100.5f;//至少移动1000米才重新通知委托处理更新 if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [_locationManager requestAlwaysAuthorization];//使用中授权 } //启动位置更新 //开启位置更新与服务器进行轮询会比较耗电,在不需要时使用stopUpdatingLocation方法关闭 [_locationManager startUpdatingLocation]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert_title_warm", @"") message:NSLocalizedString(@"alert_msg_location_disable", @"") delegate:self cancelButtonTitle:NSLocalizedString(@"sure", @"") otherButtonTitles:nil, nil]; [alert setTag: 2]; [alert show]; } } /** * 地理位置发生改变时触发 * * @param manager manager * @param newLocation newLocation * @param oldLocation oldLocation */ - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { // CLLocationCoordinate2D coordinate = newLocation.coordinate; CLLocationCoordinate2D newCoord = [self transformFromWGSToGCJ:[newLocation coordinate]]; CLLocation *transLocation = [[CLLocation alloc] initWithLatitude:newCoord.latitude longitude:newCoord.longitude]; self.lat = [NSString stringWithFormat:@"%f",transLocation.coordinate.latitude]; self.lon = [NSString stringWithFormat:@"%f",transLocation.coordinate.longitude]; debugLog(@"定位成功,经纬度:lat:%@lon:%@",self.lat,self.lon); } /** * 定位失误时触发 * * @param manager manager * @param error error */ - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { debugLog(@"定位失败,分配默认经纬度:%@", error); if (_locationManager != NULL) { [_locationManager stopUpdatingLocation]; } self.lat = @"39.955962"; self.lon = @"116.315954"; } //WGS-84 到 GCJ-02 的转换 -(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc { CLLocationCoordinate2D adjustLoc; if([self isLocationOutOfChina:wgsLoc]){ adjustLoc = wgsLoc; }else{ double adjustLat = [self transformLatWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 34.0];//35.0 double adjustLon = [self transformLonWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 34.0];//35.0 double radLat = wgsLoc.latitude / 180.0 * pi; double magic = sin(radLat); magic = 1 - ee * magic * magic; double sqrtMagic = sqrt(magic); adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi); adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi); adjustLoc.latitude = wgsLoc.latitude + adjustLat; adjustLoc.longitude = wgsLoc.longitude + adjustLon; } return adjustLoc; } //判断是不是在中国 -(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location { if (location.longitude < 72.004 || location.longitude > 137.8347 || location.latitude < 0.8293 || location.latitude > 55.8271) return YES; return NO; } -(double)transformLatWithX:(double)x withY:(double)y { double lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x)); lat += (20.0 * sin(6.0 * x * pi) + 20.0 *sin(2.0 * x * pi)) * 2.0 / 3.0; lat += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0; lat += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0; return lat; } -(double)transformLonWithX:(double)x withY:(double)y { double lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x)); lon += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0; lon += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0; lon += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0; return lon; }
4.错误解决Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed.
环境:XCODE6.0.1 + iPhone / iOS8错误:使用CoreLocation获取地理位置信息,报错
Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"
解决方法:
1.确定模拟器(手机)已经联网并且允许程序获取地理位置
2.重置地理位置服务或者网络服务
PS:如果是模拟器就果断直接重置模拟器吧 IOS Simulator - Reset Content and Settings..。
相关文章推荐
- 【学习ios之路:UI系列】获取通过UIImagePackerController获取的系统相册图片的名称信息及保存系统相册到本地
- iOS定位服务系列之一:获取当前位置信息
- 学习iOS开发之获取位置信息
- ios学习笔记: IOS8解决获取位置坐标信息出错(Error Domain=kCLErrorDomain Code=0)
- iOS菜鸟学习——获得当前位置及地理信息
- 三、ArcGIS Runtime SDK for iOS 100.2.1教程系列之图层图例信息获取
- 【IOS 开发学习总结-OC-35】★文件 I/O——NSPathUtilities.h管理路径+NSProcessInfo获取进程信息
- iOS 10获取当前定位信息,根据位置获取天气
- 【学习ios之路:UI系列】获取ios系统版本的相关操作. 及 将汉字转化为拼音功能实现
- iOS学习笔记 获取手机信息(UIDevice、NSBundle、NSLocal)
- iOS学习笔记(十三)——获取手机信息(UIDevice、NSBundle、NSLocale)
- iOS学习笔记(十三)——获取手机信息(UIDevice、NSBundle、NSLocale)
- iOS开发----地图与导航--定位和位置信息获取
- iOS开发笔记(使用Google Map Service获取用户当前位置信息)
- 微信小程序系列-1获取系统信息,地理位置,用户信息
- iOS学习笔记(十三)——获取手机信息(UIDevice、NSBundle、NSLocale)
- IOS模拟器能获取运营商信息,网络连接类型以及地理位置吗?
- iOS中获取用户所点击的位置信息,并通过这个位置信息得到tableView的中的cell的indexPath.section
- iOS项目开发实战——使用CoreLocation获取当前位置信息
- iOS学习笔记(十三)——获取手机信息(UIDevice、NSBundle、NSLocale)