美文网首页
根据经纬度 求距离

根据经纬度 求距离

作者: LuckTime | 来源:发表于2016-06-20 16:16 被阅读79次

    在Info.plist中加入两个缺省没有的字段
    NSLocationAlwaysUsageDescription
    NSLocationWhenInUseUsageDescription

    -(void)GetLonAndLat
    {
    // 2. 调用请求:
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 8.0)
    {
    //设置定位权限 仅ios8有意义
    [self.locationManager requestWhenInUseAuthorization];// 前台定位

        //  [locationManager requestAlwaysAuthorization];// 前后台同时定位
    }
    
    
    locationManager = [[CLLocationManager alloc] init];    //设置代理
    

    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);
    }

    /**

    • 计算距离
    • @param lat1 本地距离 纬度
    • @param lat2 本地距离 纬度
    • @param lng1 其他距离 经度
    • @param lng2 其他距离 经度
    • @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);//一个纬度间距离是111km
    //返回 m
    return distance;

    }

    相关文章

      网友评论

          本文标题:根据经纬度 求距离

          本文链接:https://www.haomeiwen.com/subject/lvovdttx.html