iOS定位操作和经纬度距离计算

来源:互联网 发布:mac怎么设置动态壁纸 编辑:程序博客网 时间:2024/05/28 16:26

iOS的原声定位以来CoreLocation和MapKit框架
在iOS8以后执行定位操作需要在info.plst添加两个关键字段NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription如下图:


在需要定位的地方导入#import <CoreLocation/CoreLocation.h>

//开始定位

-(void)startLocation{

    

    //判断定位操作是否被允许

    if([CLLocationManagerlocationServicesEnabled]) {

        self.locationManager = [[CLLocationManageralloc] init] ;

        self.locationManager.delegate =self;

        [self.locationManagerrequestAlwaysAuthorization];

        self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;

        self.locationManager.distanceFilter=kCLDistanceFilterNone;

        [self.locationManagerstartUpdatingLocation];

    }else {

        //提示用户无法进行定位操作

        UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:

                                  @"提示"message:@"定位不成功 ,请确认开启定位"delegate:nilcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];

        [alertView show];

    }

    

}

//定位的回调方法,

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

    

    CLLocation *oldLocation = locations[0];

    CLLocationCoordinate2D oldCoordinate = oldLocation.coordinate;

    DLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);

    

    [manager stopUpdatingLocation];//停止定位

    

    CLLocation* curLocation = [[CLLocationalloc] initWithLatitude:116.396566longitude:39.930309];

    CLLocation* otherLocation = [[CLLocationalloc] initWithLatitude:116.394158longitude:39.929957];

    double distance  = [curLocationdistanceFromLocation:otherLocation];

    NSLog(@"%f",distance);

    

    //------------------位置反编码---5.0之后使用-----------------

    CLGeocoder *geocoder = [[CLGeocoderalloc] init];

    [geocoder reverseGeocodeLocation:oldLocation

                   completionHandler:^(NSArray *placemarks,NSError *error){

                       

                       for (CLPlacemark *placein placemarks) {

                           

                           //                           UILabel *label = (UILabel *)[self.window viewWithTag:101];

                           //                           label.text = place.name;

                           NSLog(@"name,%@",place.name);                      // 位置名

                           NSLog(@"thoroughfare,%@",place.thoroughfare);      // 街道

                           NSLog(@"subThoroughfare,%@",place.subThoroughfare);// 子街道

                           NSLog(@"locality,%@",place.locality);              //

                           NSLog(@"subLocality,%@",place.subLocality);        //

                           NSLog(@"country,%@",place.country);                // 国家

                       }

                       

                   }];

   

}

警告:在模拟器定位需要给模拟器一个位置。

计算两个坐标点之间的距离

    CLLocation* curLocation = [[CLLocationalloc] initWithLatitude:116.396566longitude:39.930309];

    CLLocation* otherLocation = [[CLLocationalloc] initWithLatitude:116.394158longitude:39.929957];

    double distance  = [curLocationdistanceFromLocation:otherLocation];

默认单位为米。
0 0
原创粉丝点击