Simulator进行模拟定位

在项目中如果想要去进行定位服务,如果进行真机调试也不是很方便,我们可以利用simulator去模拟环游天朝了,具体步骤如下:

1.找到simulator的debug


2.然后修改经纬度



3.苹果提供了定位框架

#import <CoreLocation/CoreLocation.h>

一下是代码块:

- (void)locate{

    

    //判断版本

    self.locationManager = [[CLLocationManager alloc] init];

    if ([[UIDevice currentDevice].systemVersion doubleValue] >=8.0) {

        

        [self.locationManager requestAlwaysAuthorization];

    }

    if ([CLLocationManager locationServicesEnabled]) {

        

        //准确的经纬度

        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

        //精确的距离范围

        self.locationManager.distanceFilter = 10;

        self.locationManager.delegate = self;

        

    }else{

        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"定位不成功" preferredStyle:UIAlertControllerStyleAlert];

        

        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];

        

        [alertVc addAction:sureAction];

        

        [self presentViewController:alertVc animated:YES completion:nil];

    }

    //打开定位控制

    

    [self.locationManager startUpdatingLocation];

}


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

    //locations 是获取到的一些地址

    //    NSLog(@"%@",locations);

    CLLocation *currentLocation = [locations lastObject];

    //反地址编码

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    

    //通过经纬度确认城市位置

    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        

        //        NSLog(@"%@",placemarks);

        if (placemarks.count >0) {

            CLPlacemark *placeMark = [placemarks objectAtIndex:0];

            

            NSString *city = placeMark.locality;

            

            if (!city) {

                city = placeMark.administrativeArea;

            }

             if ([[city substringFromIndex:[city length] - 1] isEqualToString:@""]) {

                

                city = [city substringToIndex:[city length] - 1];

            }

            self.cityLabel.text = city;

           

        }

  

    }];

    

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值