iOS使用系统地图定位

   ios使用系统定位在ios8.0之后需要在info.plist添加NSLocationAlwaysUsageDescription与NSLocationWhenInUseUsageDescription两个key

- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorgrayColor];

    

    locationManager = [[CLLocationManageralloc]init];

    //设置代理

    locationManager.delegate =self;

    //设置定位精度

    locationManager.desiredAccuracy =kCLLocationAccuracyBest;

   //定位频率,每隔多少米定位一次

    CLLocationDistance distance =10.0;//十米定位一次

    locationManager.distanceFilter = distance;

    //启动跟踪定位

    [locationManagerstartUpdatingLocation];

    if ([CLLocationManagerauthorizationStatus] == kCLAuthorizationStatusNotDetermined){

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

            [locationManagerrequestWhenInUseAuthorization];

            [locationManagerrequestAlwaysAuthorization];

        }

    }

    if ([CLLocationManagerauthorizationStatus] == kCLAuthorizationStatusDenied) {//用户选择永不使用定位需要进行提示

        if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 9.0){

            [selfperformSelector:@selector(delayMethod)withObject:nilafterDelay:1.0];//延迟执行,否则未加载完毕viewController无法弹出报错

        }

        else{

            UIAlertView * al = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"你没有授权"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles: nil];

            [al show];

        }

    }

}

-(void)delayMethod

{

    UIAlertController * alertController = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"你没有授权"preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleCancelhandler:nil];

    [alertControlleraddAction:cancelAction];

    [selfpresentViewController:alertControlleranimated:YEScompletion:nil];

}

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

    CLLocation *location=[locationsfirstObject];//取出第一个位置

    CLLocationCoordinate2D coordinate=location.coordinate;//位置坐标

    NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);

   //如果不需要实时定位,使用完即使关闭定位服务

    [locationManagerstopUpdatingLocation];

    _geocoder=[[CLGeocoderalloc]init];

    [selfgetCoordinateByAddress:@"上海"];

    [selfgetAddressByLatitude:coordinate.latitudelongitude:coordinate.longitude];

}

-(void)getCoordinateByAddress:(NSString *)address{

    //地理编码

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

       //取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址

        CLPlacemark *placemark=[placemarksfirstObject];

        NSLog(@"详细信息:%@",placemark);

    }];

}

-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{

    //反地理编码

    CLLocation *location=[[CLLocationalloc]initWithLatitude:latitudelongitude:longitude];

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

        CLPlacemark *placemark=[placemarksfirstObject];

        NSLog(@"详细信息:%@",placemark);

    }];

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值