您的位置:首页 > 移动开发 > IOS开发

iOS学习系列——获取位置信息

2015-05-18 22:27 435 查看

1.加载动态库

定位和反查位置信息要加载两个动态库 CoreLocation.framework 和 MapKit.framework 一个获取坐标一个提供反查

2. Info.plist文件添加两个数据

NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription



3.代码(部分)

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 位置服务
相关文章推荐