iOS苹果的定位

苹果的定位是通过CoreLocation模块来实现的。

以下是一个定位的Demo。

//

//  AppleLocationSingle.m

//  bk

//

//  Created by jie on 16/4/5.

//  Copyright © 2016 jie. All rights reserved.

//

#import "AppleLocationSingle.h"

#import <UIKit/UIKit.h>


@implementation AppleLocationSingle


//定位管理的懒加载

-(CLLocationManager *)locationManger{


    if (!_locationManger) {

        _locationManger=[[CLLocationManager alloc]init];

        _locationManger.delegate=self;

        _locationManger.desiredAccuracy = kCLLocationAccuracyBest;

/*

当设置为最优的时候,苹果会根据你的精度distanceFilter大小来决定最好的方式满足你的精度。GPS最精确,其次是蜂窝基站三角定位,然后是Wifi通过网络运营商的数据库得到的数据

*/  


      _locationManger.distanceFilter = 20;//一般是能控制在20米的精度(当系统检测到你的位置没有远离上个位置20米时是不会调取你的代理)

        [_locationManger requestAlwaysAuthorization];//处于后台的情况下也可以定位。

        [_locationManger startUpdatingLocation];

    }

//判断用户是否允许当前程序使用定位服务

    if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus]==kCLAuthorizationStatusRestricted||

        [CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied) {

        //用户不允许该程序定位

        UIAlertView *tip=[[UIAlertView alloc]initWithTitle:@"提示" message:@"用户不允许定位!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];


        [tip show];

    }

    return _locationManger;

}


- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{

    

    switch (status) {

        case kCLAuthorizationStatusNotDetermined:{

            

            NSLog(@"kCLAuthorizationStatusNotDetermined");

            break;

            

        }

        case kCLAuthorizationStatusRestricted:{

            

            NSLog(@"kCLAuthorizationStatusRestricted");

            break;

        }

        case kCLAuthorizationStatusDenied:{

            

            NSLog(@"kCLAuthorizationStatusDenied");

            break;

        }

            

        case kCLAuthorizationStatusAuthorizedAlways:{

            

            NSLog(@"kCLAuthorizationStatusAuthorizedAlways");

            break;

        }


        case kCLAuthorizationStatusAuthorizedWhenInUse:{

            

            NSLog(@"kCLAuthorizationStatusAuthorizedWhenInUse");

            break;

        }

            

        default:

            NSLog(@"没有这种状态啊");

            break;

    }

    

}


- (void)locationManager:(CLLocationManager *)manager

     didUpdateLocations:(NSArray<CLLocation *> *)locations{

    NSLog(@"开始更新位置");

    

    NSLog(@"位置数组:%@",locations);

    

    CLLocation *currentLocation=locations[0];

    

    NSString *str=[NSString stringWithFormat:@"当前的坐标:%f--%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude];

    UIAlertView *tip=[[UIAlertView alloc]initWithTitle:@"定位" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

    

    [tip show];

}


- (void)locationManager:(CLLocationManager *)manager

       didFailWithError:(NSError *)error{

    NSLog(@"定位失败:%@",error);

}


@end



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值