npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cordova-ohos/cordova-plugin-geolocation

v5.0.1

Published

Cordova Geolocation Plugin

Readme

cordova-plugin-geolocation

zh-CN en

本项目基于 [email protected] 开发,本文档重点阐述其在 OpenHarmony(OHOS)系统中的具体应用。

简介

cordova-plugin-geolocation 是 Harmony Cordova 生态系统中一款核心插件,用于在 Cordova 应用中访问设备的 GPS、网络定位等功能,实现跨平台(iOS/Android/OHOS)的地理位置信息获取与监听,满足各类位置相关应用场景需求,本文档只说明在 OHOS 系统中的应用。该插件的定位是采用 W3C 定位的,实现方式:插件主要是弹窗获取用户地理位置授权后,有 webview 的定位功能实现定位,兼容 W3C。

  • 快速获取设备当前精确地理位置(纬度、经度、海拔、精度等)

  • 实时监听设备位置变化,支持动态更新位置信息

  • 可配置定位精度、超时时间、缓存策略,适配不同场景

  • 提供完善的错误处理机制,明确错误类型与解决方案

  • 严格遵循 Cordova 插件开发规范,兼容主流 Cordova 版本

支持平台

  • Android:API 19 及以上(Android 4.4+)

  • iOS:10.0 及以上

  • OHOS:5.0+

下载安装

通过 hcordova CLI 即可快速安装插件,支持从 npm 仓库或 GitCode 仓库获取。

从 npm 安装(推荐)

# 使用 hcordova CLI 安装
# 安装 hcordova 命令化工具
npm install -g hcordova
# 全平台安装插件
hcordova plugin add cordova-plugin-geolocation

指定平台安装 ohos

仅为 OHOS 平台安装插件:

# 仅安装到 OHOS 平台
hcordova plugin add cordova-plugin-geolocation --platform ohos

# 指定版本安装
hcordova plugin add [email protected] --platform ohos

从 GitCode 仓库安装

仅为 OHOS 平台安装插件:

# 仅安装到 OHOS 平台
# 默认分支安装
hcordova plugin add https://gitcode.com/CPF-Cordova/cordova-plugin-geolocation.git --platform ohos

# 指定标签/分支安装
hcordova plugin add https://gitcode.com/CPF-Cordova/cordova-plugin-geolocation.git@develop --platform ohos

离线安装(本地包)

适用于无网络环境,先下载插件包到本地,再执行离线安装:

# 下载插件包到本地(示例路径:~/Downloads/cordova-plugin-geolocation)
# 执行离线安装
hcordova plugin add ~/Downloads/cordova-plugin-geolocation --platform ohos

安装后验证

安装完成后,可通过以下命令验证插件是否成功添加到项目中:

# 查看已安装的插件列表,若包含本插件 ID 则表示插件已成功安装
hcordova plugin list

卸载

# Cordova CLI 全平台卸载
hcordova plugin remove cordova-plugin-geolocation 
# 指定平台卸载
hcordova plugin remove cordova-plugin-geolocation --platform ohos

约束与限制

依赖插件:无强制依赖,若需扩展定位相关功能,可根据实际需求搭配对应插件使用。

兼容性

支持:

| 项目 | 版本/信息 | |-----|--------| | SDK | API12+ | | IDE | DevEco Studio: 5.0+ | | ROM | 5.1+ | | Emulator | OpenHarmony 6.0+ |

在以下版本中已测试通过:

| 项目 | 版本/信息 | |-----|--------| | @cordova-ohos/ohos | 14.0.1-ohos-14.0.1 | | SDK | 5.0.0(12) | | IDE | DevEco Studio: 6.0.13.200 | | ROM | 5.1.0.120 SP3 | | Emulator | OpenHarmony 6.0.1(21) |

权限要求

使用该插件需要配置获取地理位置权限,否则无法定位

"requestPermissions": [
    {
    "name" : "ohos.permission.LOCATION",
    "reason": "$string:locationInfo", 
    "usedScene": {
        "abilities": [
        "EntryAbility"
        ],
        "when": "always"
    }
    },
    {
    "name" : "ohos.permission.APPROXIMATELY_LOCATION",
    "reason": "$string:locationInfo",
    "usedScene": {
        "abilities": [
        "EntryAbility"
        ],
        "when": "always"
    }
    },
    {
    "name" : "ohos.permission.LOCATION_IN_BACKGROUND",
    "reason": "$string:locationInfo",
    "usedScene": {
        "abilities": [
        "EntryAbility"
        ],
        "when": "always"
    }
    }
]

使用示例

获取当前位置

通过 getCurrentPosition 方法获取设备当前位置,适用于一次性定位场景(如用户签到):

// 调用获取当前位置方法
navigator.geolocation.getCurrentPosition(
 // 成功回调:获取位置信息
 function (position) {
   console.log("定位成功,位置信息如下:");
   console.log("纬度:", position.coords.latitude); // 纬度(十进制)
   console.log("经度:", position.coords.longitude); // 经度(十进制)
   console.log("定位精度:", position.coords.accuracy + " 米"); // 水平精度
   console.log("海拔:", position.coords.altitude); // 海拔(部分设备支持)
   console.log("海拔精度:", position.coords.altitudeAccuracy ); // 海拔精度
   console.log("定位时间:", new Date(position.timestamp).toLocaleString()); // 定位时间
 },
 // 失败回调:处理定位错误
 function (error) {
   console.error("定位失败:");
   switch (error.code) {
     case error.PERMISSION_DENIED:
       console.error("错误码 1:用户拒绝授予定位权限");
       break;
     case error.POSITION_UNAVAILABLE:
       console.error("错误码 2:位置信息不可用(如设备无 GPS 信号)");
       break;
     case error.TIMEOUT:
       console.error("错误码 3:定位请求超时");
       break;
     default:
       console.error("错误码 4:未知错误");
       break;
   }
 },
 // 配置选项:自定义定位参数
 {
   enableHighAccuracy: true, // 是否启用高精度定位(GPS),true = 高精度(耗电),false = 低精度(网络定位)
   timeout: 15000, // 定位超时时间(毫秒),超过时间未获取位置则触发失败回调
   maximumAge: 0 // 位置缓存有效期(毫秒),0 = 不使用缓存,30000 = 允许使用 30 秒内的缓存位置
 }
);

监听位置变化

通过 watchPosition 方法持续监听设备位置变化,适用于轨迹跟踪、实时导航等场景:

// 启动位置监听,返回监听 ID(用于后续停止监听)
var watchId = navigator.geolocation.watchPosition(
 // 成功回调:位置更新时触发
 function (position) {
   console.log("位置更新:");
   console.log("当前纬度:", position.coords.latitude);
   console.log("当前经度:", position.coords.longitude);
 },
 // 失败回调:监听错误时触发
 function (error) {
   console.error("位置监听失败:", error.message);
 },
 // 配置选项
 {
   enableHighAccuracy: false, // 低精度模式(适合后台持续监听,降低耗电)
   timeout: 30000, // 超时时间 30 秒
   maximumAge: 60000 // 允许使用 1 分钟内的缓存位置,减少重复请求
 }
);
// 停止位置监听(如页面关闭、用户停止跟踪时)
function stopLocationWatch() {
 if (watchId) {
   navigator.geolocation.clearWatch(watchId);
   watchId = null;
   console.log("位置监听已停止");
 }
}

使用说明

配置参数(options)

| 配置项 | 类型 | 默认值 | 说明 | |---|---|---|---| | enableHighAccuracy | Boolean | false | 是否启用高精度定位(true = GPS,false = 网络定位/基站定位) | | timeout | Number | Infinity | 定位超时时间(毫秒),超过时间未获取位置则触发 errorCallback | | maximumAge | Number | 0 | 位置缓存有效期(毫秒),0 = 不使用缓存,大于 0 = 允许使用指定时间内的缓存 |

新增特性

  1. 适配 OHOS 5.0+ 系统,严格遵循 OpenHarmony 开发规范;

  2. 完善权限配置说明,明确 OHOS 平台所需定位权限及配置方式;

  3. 补充详细错误处理方案,提供常见问题解决方案,提升开发体验;

  4. 兼容 W3C 定位标准,通过 webview 实现定位,保障跨平台一致性。

目录结构

cordova-plugin-geolocation/
├── src/                          # 源代码目录
│   └── main/                     # 主要源代码
│       ├── cpp/                  # C++ 原生代码
│       │   └── BaiduGeolocation/ # 百度定位模块
│       │       ├── Geolocation.cpp  # 定位功能的 C++ 实现
│       │       └── Geolocation.h    # 定位功能的头文件
│       └── ets/                  # ArkTS 代码(OpenHarmony API)
│           └── components/       # 组件目录
│               └── LocationAction/  # 定位动作组件
│                   └── LocationAction.ets  # 定位功能的 ArkTS 实现
├── www/                          # Web 资源目录
│   ├── harmony/                  # 相关资源
│   │   └──  geolocation.js       # JavaScript 定位接口(Cordova 桥接层)
│   └── PositionError.js          # 定位错误码定义
├── .gitignore                    # Git 忽略文件配置
├── LICENSE                       # 项目开源许可证
├── OAT.xml                       # OpenHarmony 审核配置文件
├── package.json                  # Node.js 包配置
├── plugin.xml                    # Cordova 插件描述文件(核心配置)
└── README.md                     # 项目说明文档

贡献代码

使用过程中发现任何问题都可以提 Issue ,当然也非常欢迎发 PR 共建。

许可证

本插件基于 Apache License 2.0 开源,详见 LICENSE 文件。

官方资源