在项目中如果想要去进行定位服务,如果进行真机调试也不是很方便,我们可以利用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;
}
}];
}