定位 CLLocationManager

兼容iOS 8.0以上获取地理位置信息

仅仅是个记录直接上代码 ~

.h 文件

1
2
3
4
5
6
7
8
9
typedef void(^GetLatitudeAndLongitude)(NSString *lat,NSString *lng);

@interface LocationManger : NSObject

@property (nonatomic, strong) NSDictionary * locationDict;

+ (instancetype)sharedLoaction;

- (void)getGps:(GetLatitudeAndLongitude)block;

.m 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@interface LocationManger ()<CLLocationManagerDelegate>
{
    GetLatitudeAndLongitude block;
}

@property (nonatomic, strong) CLLocationManager * locManager;
@end

@implementation LocationManger

+(instancetype)sharedLoaction
{
    static LocationManger *standard = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        standard = [[LocationManger alloc]init];
    });
    return standard;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        _locManager = [[CLLocationManager alloc] init];
        [_locManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [_locManager setDelegate:self];
        if([_locManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [_locManager requestAlwaysAuthorization]; // 永久授权
        }
        _locManager.distanceFilter=100;
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateToLocation:(CLLocation *)newLocation
            fromLocation:(CLLocation *)oldLocation
{
    CLLocationCoordinate2D loc = [newLocation coordinate];
    
    NSString * lat = [NSString stringWithFormat:@"%f",loc.latitude];//get latitude
    NSString *lon  = [NSString stringWithFormat:@"%f",loc.longitude];//get longitude

    if (lat!=nil && lon!=nil) {
        self.locationDict = @{@"lat":lat,
                              @"lng":lon};
        block(lat,lon);
        [manager stopUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    if ([error code] == kCLErrorDenied)
    {
        //访问被拒绝
    }
    if ([error code] == kCLErrorLocationUnknown) {
        //无法获取位置信息
    }
}

- (void)getGps:(GetLatitudeAndLongitude)myblock
{
    block = myblock;
    if ([CLLocationManager locationServicesEnabled] == FALSE) {
        return;
    }
    [_locManager startUpdatingLocation];
   
}
@end

还要在info.plist文件里创建NSLocationAlwaysUsageDescription这一项,对应的值不能乱写,它会在询问是否允许定位中显示出来,这样写 ~ 比如我们需要获取您的地理位置

这么调用 ~ 

1
2
[[LocationManger sharedLoaction] getGps:^(NSString *lat, NSString *lng) {
}];
分享到 评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp 是一个跨平台的应用开发框架,需要在不同的平台上实现后台定位保活可能会有些不同。一般来说,在 Android 平台上可以通过 Service 实现后台定位保活,而在 iOS 平台上则需要使用后台任务和后台模式。 在 Android 平台上,可以创建一个 Service,在其中开启一个线程进行定位,保证定位服务一直在后台运行。同时,需要在 AndroidManifest.xml 文件中申明该 Service 的启动方式为 START_STICKY,这样当系统回收资源时,会自动重新启动该 Service。 在 iOS 平台上,需要使用后台任务和后台模式来实现后台定位保活。可以使用 Core Location 框架来实现定位功能,在开启定位时,需要在 Info.plist 文件中加入 NSLocationAlwaysUsageDescription 权限申明,以获取后台定位权限。同时,需要在 AppDelegate.m 文件中申明后台任务和后台模式,例如: ``` - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 启用后台任务和后台模式 [self enableBackgroundTaskAndMode]; return YES; } - (void)enableBackgroundTaskAndMode { // 申明后台任务 self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [self endBackgroundTask]; }]; // 申明后台模式 CLLocationManager *locationManager = [[CLLocationManager alloc] init]; [locationManager setAllowsBackgroundLocationUpdates:YES]; } ``` 需要注意的是,在 iOS 平台上,由于苹果的限制,后台定位保活可能会被系统杀掉,因此需要在代码中添加相应的处理逻辑,例如重新开启定位等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值