Simulator进行模拟定位

来源:互联网 发布:韩语网络教学 编辑:程序博客网 时间:2024/05/03 08:25

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

1.找到simulator的debug


2.然后修改经纬度



3.苹果提供了定位框架

#import <CoreLocation/CoreLocation.h>

一下是代码块:

- (void)locate{

    

    //判断版本

    self.locationManager = [[CLLocationManageralloc] init];

    if ([[UIDevicecurrentDevice].systemVersiondoubleValue] >=8.0) {

        

        [self.locationManagerrequestAlwaysAuthorization];

    }

    if ([CLLocationManagerlocationServicesEnabled]) {

        

        //准确的经纬度

        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

        //精确的距离范围

        self.locationManager.distanceFilter = 10;

        self.locationManager.delegate = self;

        

    }else{

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

        

        UIAlertAction *sureAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:nil];

        

        [alertVc addAction:sureAction];

        

        [selfpresentViewController:alertVcanimated:YEScompletion:nil];

    }

    //打开定位控制

    

    [self.locationManagerstartUpdatingLocation];

}


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

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

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

    CLLocation *currentLocation = [locationslastObject];

    //反地址编码

    CLGeocoder *geocoder = [[CLGeocoderalloc] init];

    

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

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

        

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

        if (placemarks.count >0) {

            CLPlacemark *placeMark = [placemarksobjectAtIndex:0];

            

            NSString *city = placeMark.locality;

            

            if (!city) {

                city = placeMark.administrativeArea;

            }

             if ([[citysubstringFromIndex:[citylength] - 1] isEqualToString:@""]) {

                

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

            }

            self.cityLabel.text = city;

           

        }

  

    }];

    

}



0 0
原创粉丝点击