iOS, corelocation定位经纬度坐标,以及经纬度距离

来源:互联网 发布:unity3d中国官网 编辑:程序博客网 时间:2024/06/14 16:06


首先iOS定位用到框架CoreLocation.framework

导入

#import <CoreLocation/CoreLocation.h>


实现 代理 <CLLocationManagerDelegate>


CLLocationManager* locationmanager;  创建一个CLLocationManager类


//进程序的时候打开定位

- (void)viewWillAppear:(BOOL)animated

{

    [superviewWillAppear:animated];

    [locationmanagerstartUpdatingLocation];

}


//退出程序的时候关闭定位  这样做优化程序使用

- (void)viewWillDisappear:(BOOL)animated

{

    [superviewWillDisappear:animated];

    [locationmanagerstopUpdatingLocation];

}


- (void)viewDidLoad

{

    [superviewDidLoad];


    locationmanager = [[CLLocationManageralloc]init];//创建

    locationmanager.delegate =self;//设置代理

    locationmanager.desiredAccuracy =kCLLocationAccuracyNearestTenMeters;//设置精确度

    locationmanager.distanceFilter =100.f;//设置kCLDistanceFilterNone 为一秒刷新一次

    

    

    // 设置定位精度

    // kCLLocationAccuracyNearestTenMeters:精度10

    // kCLLocationAccuracyHundredMeters:精度100

    // kCLLocationAccuracyKilometer:精度1000

    // kCLLocationAccuracyThreeKilometers:精度3000

    // kCLLocationAccuracyBest:设备使用电池供电时候最高的精度

    // kCLLocationAccuracyBestForNavigation:导航情况下最高精度,一般要有外接电源时才能使用

    

}


#pragma mark 代理方法


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

   CLLocation * currentLocation = [locations lastObject];

    CLLocationCoordinate2D coor = currentLocation.coordinate;

   double  altitude = currentLocation.altitude;//高度

   double  latitude = coor.latitude;

   double  longitude = coor.longitude;

    

    

    NSLog(@"altitude == %f",altitude);//高度

    NSLog(@"latitude == %f",latitude);//纬度

    NSLog(@"longitude == %f",longitude);//经度

    

    

    

#pragma mark 计算位置距离-1

    

   double dd = M_PI/180;

   double x1=31.268980*dd,x2=latitude*dd;

   double y1=121.440119*dd,y2=longitude*dd;

   double R = 6371004;

   double distance = (2*R*asin(sqrt(2-2*cos(x1)*cos(x2)*cos(y1-y2) - 2*sin(x1)*sin(x2))/2));


   distance_Label.text = [NSStringstringWithFormat:@"距离 = %f",distance];

    

    

    


    

   float  ditance = ((acos(sin(31.268980 *M_PI / 180.0) *sin(latitude * M_PI /180.0)+ cos(31.268980 *M_PI / 180.0) *cos(latitude * M_PI /180.0) * cos((121.440119 - longitude) *M_PI / 180.0)) *180.0 / M_PI) *111189.57696);

    

   NSLog(@"sf距离 = %f",ditance);

   SFdistance_Label.text = [NSStringstringWithFormat:@"sf距离 = %f",distance];

    

    

    

//    ((acos(sin(x1 * M_PI / 180.0) * sin(x2 * M_PI / 180.0)+ cos(x1 * M_PI / 180.0) * cos(x2 * M_PI / 180.0) * cos((y1 - y2) * M_PI / 180.0)) * 180.0 / M_PI) * 111189.57696)

    

    

//    double dd = M_PI/180;

//    double x1=lat1*dd,x2=lat2*dd;

//    double y1=lng1*dd,y2=lng2*dd;

//    double R = 6371004;

//    double distance = (2*R*asin(sqrt(2-2*cos(x1)*cos(x2)*cos(y1-y2) - 2*sin(x1)*sin(x2))/2));

    

    

}





0 0
原创粉丝点击