IOS定位及编译位置为中文

这篇博客介绍了如何在iOS应用中使用CoreLocation框架和MapKit进行定位。首先引入了CoreLocation和MapKit框架,然后创建CLLocationManager实例并设置代理,确保定位服务开启。接着配置定位精度和距离过滤器,并在定位成功或失败时调用相应的代理方法。当定位成功时,通过CLGeocoder反向地理编码获取地址信息,提取省、市等详细地理位置数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导入包:CoreLocation.framework    Mapkit.frameWork

 

#import <CoreLocation/CoreLocation.h>

#import <MapKit/MapKit.h>

 

CLLocationManagerDelegate

 

@property(nonatomic,strong)CLLocationManager*  locationManager;        //位置信息

 

 

 

#pragma mark -

#pragma mark - CLLocationManager delegate

 

//开始定位

-(void)startLocation{

   

    [StaticTools showHUDInView:self.view HudType:MBProgressHUDModeIndeterminate withText:@"定位中"];

   

    if (![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied) {

       

        [StaticTools showAlertWithTag:Alert_Tag_CloseLocationServices title:nil message:@"定位功能未开启"  AlertType:CAlertTypeDefault SuperView:self];

                                               //确定

       

    }else{

       

        NSLog(@"定位正常");

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

        self.locationManager.delegate = self;

        //最佳精度,最耗电

        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

        //距离过滤,超出设定的方圆100m,就会重新校正当前用户的位置信息

        self.locationManager.distanceFilter = 1000.0f;

        //发送startupdatingLocation

        [self.locationManager startUpdatingLocation];

       

}

}

 

//定位出错,经纬度出错

-(void)locationManager:(CLLocationManager *)managerdidFailWithError:(NSError *)error{

    [StaticTools removeHUDFromView:self.view];

    [self.locationManager stopUpdatingLocation];

    NSLog(@"获取经纬度失败,失败原因是%@",[error description]);

    [StaticTools showAlertWithTag:Alert_Tag_LocationFail title:nil message:@"定位失败!"  AlertType:CAlertTypeDefault SuperView:self];

}

 

//定位成功时调用

-(void)locationManager:(CLLocationManager *)managerdidUpdateLocations:(NSArray *)locations

{

    [StaticTools removeHUDFromView:self.view];

    [self.locationManager stopUpdatingLocation];

    CLLocation *curr = [locations lastObject];

    CLLocationCoordinate2D coord;

    NSLog(@"1--%f=====%f",curr.coordinate.latitude,curr.coordinate.longitude);

   

    coord = curr.coordinate;

    NSLog(@"2--%f=====%f",coord.latitude,coord.longitude);

    CLLocation *location = [[CLLocation alloc]initWithCoordinate:coord altitude:curr.altitude horizontalAccuracy:curr.horizontalAccuracy verticalAccuracy:curr.verticalAccuracy course:curr.course speed:curr.speed timestamp:curr.timestamp];

   

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

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

        for (CLPlacemark *placemark in placemarks) {

            NSLog(@"3--%f====dandan=%f",location.coordinate.latitude,location.coordinate.longitude);

            NSLog(@"placemark==dandan=%@",placemark);

            //省:

            NSString* str=[placemark.addressDictionary objectForKey:@"State"];

           NSString* str2=[placemark.addressDictionary objectForKey:@"City"];

           

            if ([str rangeOfString:@""].location!=NSNotFound) {

                NSString* state=str;

                ShareManager.fixedState=state;

                NSLog(@"4------------%@",ShareManager.fixedState);

            }else{

                NSString* state=[str substringToIndex:[str length]-1];

                ShareManager.fixedState=state;

                NSLog(@"4------111111------%@",ShareManager.fixedState);

            }

           

            //市:

           

            if ([str2 rangeOfString:@""].location!=NSNotFound) {

                NSString* city=str2;

                ShareManager.fixedCity=city;

                NSLog(@"5------------%@",ShareManager.fixedCity);

            }else{

                NSString* city=[str substringToIndex:[str length]-1];

                ShareManager.fixedCity=city;

                NSLog(@"5------111111------%@",ShareManager.fixedCity);

               

               

            }

           

        }

    }];

   

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值