GithubHelp home page GithubHelp logo

laixiangran / cordova-plugin-bd-geolocation Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 2.0 1.4 MB

Baidu Geolocation for Cordova(基于百度定位SDK 7.3,实现前台定位通知服务来适配 android 8.0 对后台定位的限制)

License: MIT License

Java 92.79% JavaScript 7.21%
cordova baidu-geolocation position

cordova-plugin-bd-geolocation's Introduction

Baidu Geolocation for Cordova

百度 Android 定位SDK

Baidu 全量定位 SDK 版本:v7.5(全量定位:包含离线定位、室内高精度定位能力,同时提供更人性化的位置描述服务;)

Cordova 百度定位插件,兼容 W3C 的 geolocation 标准,解决**大陆手机无法定位的问题。

基于ETENG-OSP的成果进行修改,非常感谢!

重要

为了适配 Android 8.0 对后台定位的限制,特根据sdk新增 openFrontLocationServicecloseFrontLocationService 两个方法,详细用法见后面的说明。

官方说明:http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/addition-func/android8-notice

安装

在控制台里,进入 cordova 项目目录,执行以下命令:

cordova plugin add cordova-plugin-bd-geolocation --variable API_KEY=百度分配的AK --save

如果需要同时在 iOS 里和 Android 里使用,请在 config.xml 里分别配置:

...
  <!-- android 使用本插件 -->
  <platform name="android">
    <plugin name="cordova-plugin-bd-geolocation" spec="^8.5.0">
        <variable name="API_KEY" value="百度分配的AK" />
    </plugin>
  </platform>

  <!-- iOS 使用官方插件 -->
  <platform name="ios">
    <plugin name="cordova-plugin-geolocation" spec="^4.0.1" />
  </platform>
...

卸载

在控制台里,进入 cordova 项目目录,执行以下命令:

cordova plugins remove cordova-plugin-bd-geolocation

关于 API_KEY

使用前需要在百度申请应用,获取 API_KEY。填错了的话仅能使用 GPS 定位,无法使用基站与 WIFI 定位。

使用方法

navigator.geolocation.getCurrentPosition(success, [error], [options]);

获取当前位置

options

var options = {
  enableHighAccuracy: true,  // 是否使用 GPS
  maximumAge: 30000,         // 缓存时间
  timeout: 27000,            // 超时时间
  coorType: 'bd09ll'         // 默认是 gcj02,可填 bd09ll 以获取百度经纬度用于访问百度 API
}

success

function success(position, [extra]) {
}
position
{
  "coords": {
    "latitude": "number", // 十进制数的纬度
    "longitude": "number", // 十进制数的经度
    "altitude": "number", // 海拔,海平面以上以米计
    "accuracy": "number", // 位置精度
    "altitudeAccuracy": "number", // 位置的海拔精度
    "heading": "number", // 方向,从正北开始以度计
    "speed": "number" // 速度,以米/每秒计
  },
  "timestamp": "number" // 响应的日期/时间
}
extra
{
  "type": "number", // 定位类型。161:网络定位结果,61:GPS定位结果,66:离线定位结果
  "gpsAccuracyStatus": "number", // GPS质量。0:GPS质量判断未知,1:GPS质量判断好,2:GPS质量判断中等,3:GPS质量判断差
  "addr": "string" // 详细地址信息
}

注意:由于 W3C 的 geolocation 标准中getCurrentPosition的success回调函数其实是返回一个参数position,因此如果你在typescript中使用,直接写成接收两个参数则会编译出错,因此你可以这样写:

navigator.geolocation.watchPosition((...args: any[]) => {
    const position: Position = args[0];
    const extra: any = args[1];
    console.log(position);
    console.log(extra);
}, () => {}, options);

navigator.geolocation.watchPosition(success, [error], [options]);

持续追踪位置变更,参数与getCurrentPosition一致。

返回值:watchId

navigator.geolocation.clearWatch(watchId);

清除位置追踪。

navigator.geolocation.openFrontLocationService(watchId)

开启前台定位通知服务。适配 android 8.0 以上对后台定位的限制。判断当前应用在后台运行,则调用该方法。

navigator.geolocation.closeFrontLocationService(watchId)

关闭前台定位通知服务。判断当前应用不在后台运行,则调用该方法。

关于坐标系

由于 Baidu 定位的限制,这个插件仅能获取**偏移坐标系 GCJ02 与 BD09LL(LL 指代经纬度)。如果需要坐标系的转换,请使用第三方服务。

如果期望离线转换坐标系,可以使用这个算法:https://github.com/googollee/eviltransform

这个插件不对转换的结果负责。

cordova-plugin-bd-geolocation's People

Contributors

laixiangran avatar

Stargazers

 avatar hllinc avatar  avatar Chuancan Li avatar zhoucl avatar crapthings avatar

Watchers

James Cloos avatar  avatar

Forkers

hllinc 19317362

cordova-plugin-bd-geolocation's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.