根據(jù)經(jīng)緯度 求距離

在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 distance
1000;
NSLog(@"distance距離%f; lat1= %f; lat2 =%f; lng1 = %f; lng2 = %f ",distance,lat1,lat2,lng1,lng2);//一個(gè)緯度間距離是111km
//返回 m
return distance;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容