在Info.plist中加入兩個(gè)缺省沒(méi)有的字段
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
-(void)GetLonAndLat
{
// 2. 調(diào)用請(qǐng)求:
if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 8.0)
{
//設(shè)置定位權(quán)限 僅ios8有意義
[self.locationManager requestWhenInUseAuthorization];// 前臺(tái)定位
// [locationManager requestAlwaysAuthorization];// 前后臺(tái)同時(shí)定位
}
locationManager = [[CLLocationManager alloc] init]; //設(shè)置代理
locationManager.delegate = self;
[CLLocationManager locationServicesEnabled];
locationManager.desiredAccuracy=kCLLocationAccuracyThreeKilometers;
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation *cl = [locations objectAtIndex:0];
self.loX = [[NSString stringWithFormat:@"%f",cl.coordinate.longitude] doubleValue];
self.LoY = [[NSString stringWithFormat:@"%f",cl.coordinate.latitude] doubleValue];
}
//-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
// NSLog(@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
//}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"%@",error);
}
/**
- 計(jì)算距離
- @param lat1 本地距離 緯度
- @param lat2 本地距離 緯度
- @param lng1 其他距離 經(jīng)度
- @param lng2 其他距離 經(jīng)度
- @return
*/
+(double)distanceBetweenOrderBy:(double)lat1 :(double)lat2 :(double)lng1 :(double)lng2{
double dd = M_PI/180;
double x1=lat1dd,x2=lat2dd;
double y1=lng1dd,y2=lng2dd;
double R = 6371004;
double distance = (2Rasin(sqrt(2-2cos(x1)cos(x2)cos(y1-y2) - 2sin(x1)sin(x2))/2)) / 1000;
//km 返回
// return distance1000;
NSLog(@"distance距離%f; lat1= %f; lat2 =%f; lng1 = %f; lng2 = %f ",distance,lat1,lat2,lng1,lng2);//一個(gè)緯度間距離是111km
//返回 m
return distance;
}