1. 插件介绍

Flutter Geolocator 是一个功能强大的地理位置插件,它提供了对平台特定位置服务的便捷访问。fluttertpc_geolocator 是专为鸿蒙系统适配的版本,基于原生 geolocator 插件开发,提供了在鸿蒙平台上获取设备位置信息的完整功能。

主要功能

  • 获取设备的当前位置
  • 获取设备的最后已知位置
  • 检查定位服务是否启用
  • 检查和请求定位权限
  • 打开应用设置和定位设置
  • 获取位置精度信息
  • 支持鸿蒙平台特定配置

2. 安装与配置

2.1 Git 依赖引入

由于这是专为鸿蒙系统适配的自定义修改版本,需要通过 Git 形式引入依赖。在项目的 pubspec.yaml 文件中添加以下配置:

dependencies:
  geolocator:
    git:
      url: https://gitcode.com/openharmony-sig/fluttertpc_geolocator.git
      path: "geolocator"

添加依赖后,执行以下命令获取依赖包:

flutter pub get

2.2 鸿蒙平台权限配置

在鸿蒙平台上使用定位功能需要配置相应的权限。请按照以下步骤进行配置:

2.2.1 配置模块权限

打开 entry/src/main/module.json5 文件,添加以下权限配置:

"requestPermissions": [
  {"name": "ohos.permission.INTERNET"},
  {
    "name": "ohos.permission.LOCATION",
    "reason": "$string:location",
    "usedScene": {
      "abilities": ["FormAbility"],
      "when": "inuse"
    }
  },
  {
    "name": "ohos.permission.LOCATION_IN_BACKGROUND",
    "reason": "$string:locationbackground",
    "usedScene": {
      "abilities": ["FormAbility"],
      "when": "inuse"
    }
  },
  {
    "name": "ohos.permission.APPROXIMATELY_LOCATION",
    "reason": "$string:locationapprox",
    "usedScene": {
      "abilities": ["FormAbility"],
      "when": "inuse"
    }
  },
  {
    "name": "ohos.permission.KEEP_BACKGROUND_RUNNING"
  }
]
2.2.2 添加权限说明

打开 entry/src/main/resources/base/element/string.json 文件,添加以下权限说明:

{
  "string": [
    {
      "name": "location",
      "value": "使用定位功能"
    },
    {
      "name": "locationbackground",
      "value": "在后台使用定位功能"
    },
    {
      "name": "locationapprox",
      "value": "使用近似位置功能"
    }
  ]
}
2.2.3 应用权限等级配置

由于部分权限属于 system_basic 等级,需要将应用的权限等级修改为 system_basic。请参考 华为官方文档 进行配置。

3. API 调用示例

3.1 基本使用流程

在使用 Geolocator 插件之前,建议先检查定位服务是否启用以及是否拥有相应的权限:

import 'package:geolocator/geolocator.dart';

Future<bool> _handlePermission() async {
  bool serviceEnabled;
  LocationPermission permission;

  // 检查定位服务是否启用
  serviceEnabled = await Geolocator.isLocationServiceEnabled();
  if (!serviceEnabled) {
    // 定位服务未启用,提示用户启用
    return false;
  }

  // 检查定位权限
  permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied) {
    // 请求定位权限
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.denied) {
      // 权限被拒绝
      return false;
    }
  }

  if (permission == LocationPermission.deniedForever) {
    // 权限被永久拒绝,引导用户手动设置
    return false;
  }

  // 权限已授予,可以访问设备位置
  return true;
}

3.2 获取当前位置

Future<void> _getCurrentPosition() async {
  final hasPermission = await _handlePermission();
  if (!hasPermission) {
    return;
  }

  // 获取当前位置
  Position position = await Geolocator.getCurrentPosition(
    desiredAccuracy: LocationAccuracy.high
  );

  print('当前位置: ${position.latitude}, ${position.longitude}');
  print('海拔高度: ${position.altitude} 米');
  print('位置精度: ${position.accuracy} 米');
}

3.3 获取最后已知位置

Future<void> _getLastKnownPosition() async {
  final hasPermission = await _handlePermission();
  if (!hasPermission) {
    return;
  }

  // 获取最后已知位置(可能返回 null)
  Position? position = await Geolocator.getLastKnownPosition();

  if (position != null) {
    print('最后已知位置: ${position.latitude}, ${position.longitude}');
  } else {
    print('没有可用的最后已知位置');
  }
}

3.4 检查定位服务状态

Future<void> _checkLocationServiceStatus() async {
  bool isEnabled = await Geolocator.isLocationServiceEnabled();
  print('定位服务状态: ${isEnabled ? '已启用' : '已禁用'}');
}

3.5 打开应用设置和定位设置

// 打开应用设置
Future<void> _openAppSettings() async {
  bool opened = await Geolocator.openAppSettings();
  print('应用设置是否打开: $opened');
}

// 打开定位设置
Future<void> _openLocationSettings() async {
  bool opened = await Geolocator.openLocationSettings();
  print('定位设置是否打开: $opened');
}

3.6 鸿蒙平台特殊配置

鸿蒙平台支持一些特殊的配置选项,可以通过 OhosSettings 类进行设置:

import 'package:flutter_geolocator_ohos/flutter_geolocator_ohos.dart';

final ohosSettings = OhosSettings(
  accuracy: LocationAccuracy.best, // 定位精度
  distanceFilter: 10, // 距离过滤(米)
  intervalDuration: const Duration(seconds: 1), // 更新间隔
  forceLocationManager: false, // 是否强制使用 LocationManager
  useMSLAltitude: true, // 是否使用平均海平面海拔
  foregroundNotificationConfig: const ForegroundNotificationConfig(
    notificationText: "应用将在后台继续接收位置信息",
    notificationTitle: "后台运行中",
    enableWakeLock: false, // 是否保持设备唤醒
  ),
);

// 使用鸿蒙特定配置获取位置流
final positionStream = Geolocator.getPositionStream(
    locationSettings: ohosSettings);

4. 支持的 API

以下是鸿蒙平台支持的主要 API 列表:

API 方法 功能描述 支持状态
checkPermission() 检查定位权限 ✅ 支持
requestPermission() 请求定位权限 ✅ 支持
isLocationServiceEnabled() 检查定位服务是否启用 ✅ 支持
getLastKnownPosition() 获取最后已知位置 ✅ 支持
getLocationAccuracy() 获取位置精度 ✅ 支持
getCurrentPosition() 获取当前位置 ✅ 支持
openAppSettings() 打开应用设置 ✅ 支持
openLocationSettings() 打开定位设置 ✅ 支持
getServiceStatusStream() 监听服务状态变化 ❌ 不支持
getPositionStream() 监听位置变化 ❌ 不支持

5. 总结

fluttertpc_geolocator 是一个功能完整的 Flutter 地理位置插件,专为鸿蒙系统进行了适配。它提供了获取设备位置、检查和请求权限、管理定位设置等核心功能,满足大多数应用对地理位置服务的需求。

使用该插件时,需要注意以下几点:

  1. 通过 Git 形式引入依赖,确保使用的是鸿蒙适配版本
  2. 正确配置鸿蒙平台所需的定位权限
  3. 在使用定位功能前,检查定位服务是否启用以及是否拥有相应权限
  4. 根据需要使用鸿蒙平台的特殊配置选项

通过本指南,您应该已经了解了如何在鸿蒙平台上安装、配置和使用 Flutter Geolocator 插件。祝您开发顺利!

欢迎加入开源鸿蒙跨平台社区: https://openharmonycrossplatform.csdn.net

Logo

作为“人工智能6S店”的官方数字引擎,为AI开发者与企业提供一个覆盖软硬件全栈、一站式门户。

更多推荐