GPS学习

1、每一个你不满意的现在,都有一个你没有努力的曾经。

//ios根据gps坐标来计算两点间的距离

//x1,y1 点1的坐标 x2,y2点2的坐标

-(double) gps2m:(double)x1 _y1:(double)y1 _x2:(double)x2 _y2:(double)y2

{

double radLat1 = (x1 * 3.1416 / 180.0);

double radLat2 = (x2 * 3.1416 / 180.0);

double a = radLat1 - radLat2;

double b = (y1 - y2) * 3.1416 / 180.0;

double s = 2 * asin(sqrt(pow(sin(a / 2), 2) + cos(radLat1) * cos(radLat2) * pow(sin(b / 2), 2)));

s = s * 6378137.0;

s = round(s * 10000) / 10000;

return s;

}

如果是xcode6和ios 8的话,需要调用 CLLocationManager requestAlwaysAuthorization 方法,具体步骤如下:

1. @interface里:
  CLLocationManager *locationManager;
2. 初始化:
  locationManager = [[CLLocationManager alloc] init];
3. 调用请求:
  [locationManager requestAlwaysAuthorization];
  [locationManager startUpdatingLocation];
4. 在 info.plist里加入:
  NSLocationWhenInUseDescription,允许在前台获取GPS的描述
  NSLocationAlwaysUsageDescription,允许在后台获取GPS的描述

ps:调用请求前加上ios版本判断就完美了。

完美一下:

// 判斷是否 iOS 8
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])

{
  [self.locationManager requestAlwaysAuthorization];      // 永久授权
  [self.locationManager requestWhenInUseAuthorization];   //使用中授权
}
[self.locationManager startUpdatingLocation];

上一篇:[译]ES6特性


下一篇:【Java基础】static关键字相关