更新时间:2026/11/30 12:05
后台持续定位
功能场景
本示例展示了在应用退后台的状态下,如何持续记录位置信息,增加定位进程存活率。
扫码体验

核心接口
| 类 | 接口 | 描述 |
|---|---|---|
| LocationClient | start() | 启动定位sdk |
| LocationClient | setLocOption(LocationClientOption locOption) | 给定位客户端设置参数 |
| LocationClient | enableLocInForeground(int id, android.app.Notification notification) | 将定位SDK的SERVICE设置成为前台服务, 提高定位进程存活率 |
| LocationClient | disableLocInForeground(boolean removeNotification) | 关闭前台定位服务 |
| LocationClientOption | setScanSpan(int scanSpan) | 连续定位时间间隔,单位是毫秒 当<1000(1s)时,定时定位无效 |
| BDAbstractLocationListener | onReceiveLocation(BDLocation location) | 监听定位结果回调函数 |
扫码体验

核心接口
| 类 | 接口 | 描述 |
|---|---|---|
| BMKLocationManager | - (void)startUpdatingLocation; | 开始连续定位 |
| BMKLocationManager | - (void)stopUpdatingLocation; | 停止连续定位 |
| BMKLocationManager | - (void)BMKLocationManager:(BMKLocationManager * _Nonnull)manager doRequestAlwaysAuthorization:(CLLocationManager * _Nonnull)locationManager; | 后台定位权限请求。为了适配app store关于新的后台定位的审核机制当开发者在plist配置NSLocationAlwaysUsageDescription或者NSLocationAlwaysAndWhenInUseUsageDescription时,///需要在该delegate中调用后台定位api:[locationManager requestAlwaysAuthorization] |
| BMKLocationManager | - (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error; | 连续定位回调函数 |
Android 示例
重点关注
为了保证定位可以在后台一直运行,开发者可以通过开启前台服务,来保证SDK服务的存活,达到后台定位效果。
核心代码
1.创建前台通知服务
Java
/*** 初始化前台服务*/private void initNotification () { //设置后台定位 //android8.0及以上使用NotificationUtils if (Build.VERSION.SDK_INT >= 26) { NotificationUtils notificationUtils = new NotificationUtils(this); Notification.Builder builder = notificationUtils.getAndroidChannelNotification ("适配android 8限制后台定位功能", "正在后台定位"); mNotification = builder.build(); } else { //获取一个Notification构造器 Notification.Builder builder = new Notification.Builder(MainActivity.this); Intent nfIntent = new Intent(MainActivity.this, MainActivity.class);
builder.setContentIntent(PendingIntent. getActivity(MainActivity.this, 0, nfIntent, 0)) // 设置PendingIntent .setContentTitle("适配android 8限制后台定位功能") // 设置下拉列表里的标题 .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标 .setContentText("正在后台定位") // 设置上下文内容 .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间
mNotification = builder.build(); // 获取构建好的Notification } mNotification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音}2.将sdk服务设置为前台通知服务
Java
// 将定位SDK的SERVICE设置成为前台服务, 提高定位进程存活率mClient.enableLocInForeground(1, mNotification);3.创建后台定位监听
Java
class MyLocationListener extends BDAbstractLocationListener { @Override public void onReceiveLocation(BDLocation bdLocation) { // 此处获取后台定位数据 }}4.配置后台连续定位参数并启动定位
Java
// 创建定位客户端mClient = new LocationClient(this);myLocationListener = new MyLocationListener();// 注册定位监听mClient.registerLocationListener(myLocationListener);LocationClientOption mOption = new LocationClientOption();// 可选,默认0,即仅定位一次,设置发起连续定位请求的间隔需要大于等于1000ms才是有效的mOption.setScanSpan(2000);// 可选,默认gcj02,设置返回的定位结果坐标系,如果配合百度地图使用,建议设置为bd09ll;mOption.setCoorType("bd09ll");// 可选,默认false,设置是否开启Gps定位mOption.setOpenGps(true);// 设置定位参数mClient.setLocOption(mOption);// 启动定位mClient.start();iOS 示例
重点关注
为了保证定位可以在后台一直运行,开发者需配置info.pist、 申请后台定位权限、开启 TARGETS->Capabilities->Background Modes,来保证SDK服务的存活,达到后台定位效果。
核心代码
1.创建定位管理类对象,关键属性allowsBackgroundLocationUpdates设置为YES
Objective-C
_locationManager = [[BMKLocationManager alloc] init];// 设置返回位置的坐标系类型_locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;// 设置距离过滤参数_locationManager.distanceFilter = kCLDistanceFilterNone;// 设置预期精度参数_locationManager.desiredAccuracy = kCLLocationAccuracyBest;// 设置应用位置类型_locationManager.activityType = CLActivityTypeAutomotiveNavigation;// 设置是否自动停止位置更新_locationManager.pausesLocationUpdatesAutomatically = NO;
//设置是否允许后台定位_locationManager.allowsBackgroundLocationUpdates = YES;// 设置位置获取超时时间_locationManager.locationTimeout = 10;// 设置获取地址信息超时时间_locationManager.reGeocodeTimeout = 10;// 设置delegate_locationManager.delegate = self;2.请求后台定位权限
Objective-C
- (void)BMKLocationManager:(BMKLocationManager * _Nonnull)manager doRequestAlwaysAuthorization:(CLLocationManager * _Nonnull)locationManager { [locationManager requestAlwaysAuthorization];}3.实现定位回调代理方法
Objective-C
- (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error { // 定位错误信息 if (error) { NSLog(@"locError:{%zd -%@};", error.code, error.localizedDescription); return; }
// 定位结果 if (location) { // 获取定位经纬度 CLLocationCoordinate2D coord = location.location.coordinate; // 更新当前位置数据 BMKUserLocation *userLocation = [[BMKUserLocation alloc] init]; userLocation.location = location.location; // 如需展示我的位置图标需将BMKMapView属性showsUserLocation设置为YES,并调用此方法 [self.mapView updateLocationData:userLocation]; }}4.开始后台定位
Objective-C
[self.locationManager startUpdatingLocation];5.停止后台定位
Objective-C
[self.locationManager stopUpdatingLocation];本篇文章对您是否有帮助?