Giter Club home page Giter Club logo

cordova-qdc-baidu-location's Introduction

#百度地图定位Android版插件 本插件利用百度地图提供的定位功能进行Android版手机定位。

####改动了哪些?

  1. 原插件是否成功定位没有统一标识,增加了status属性(可看后续例子)
  2. 更新到百度最新SDK
  3. 修正了定位失败的错误描述。其实原版本是直接参照百度范例来的,百度范例本身就不准确,我根据文档逐个核实定位类型,修改了错误描述,使之更准确。
  4. 把原版本的lontitude改为longitude,可能是拼写错误吧。

####为什么没有iOS版? 因为iOS版有官方的定位插件_cordova-plugin-geolocation_可以使用。 补充:使用百度经纬应该还是能精确些的,后续加上吧

请参照:cordova-plugin-geolocation

####Android版为什么不使用官方的_cordova-plugin-geolocation_插件 最新版的插件已经删除掉的Android版定位的代码,改为基于系统浏览器(chrome内核)进行定位。

为什么这样做,也有人问过同样的问题,作者的回答是这样比原生定位更快更准确。

但经过测试后,发现根本无法定位,几经调查发现跟貌似国内网络有关系,原因相信大家都懂的,此过省略好几个字。。。。 补充:在实际开发过程中,大部分手机能定位,但也存在无法经纬的情况,所以转到了百度定位。经纬经度也不需要额外调用其他接口了。

此插件就这么诞生了

####版本 基于百度地图Android版定位SDK(v7.1)

####一,申请密钥 请参照:申请密钥Android定位SDK

####二,安装插件

ionic plugin add https://github.com/chenxinbin/cordova-qdc-baidu-location --variable API_KEY="<API_KEY>"
**注:此处的API_KEY来自于第一步,不带左右尖括号**

####三,使用方法

// 进行定位
baidu_location.getCurrentPosition(successCallback, failedCallback);

获得定位信息,返回JSON格式数据:

{
  latitude : 纬度,
  longitude: 经度,(补充:原git中为lontitude,相信是拼写错误)
  ...
}

具体字段内容请参照:BDLocation

####四,查看当前安装了哪些插件

ionic plugin ls

####五,删除插件

ionic plugin rm com.qdc.plugins.baidu.location

####六. Ionic 2 实例

declare let baidu_location; //import后马上声明

  getCurrentPosition()
  {
      //console.log("android");
      if(this.platform.is("android"))
          return new Promise(
          function(resolve, reject) {
              baidu_location.getCurrentPosition(
                function(data){
                    var coords = JSON.parse(data);
                    if(coords["status"]=='SUCCESS')
                    {
                        resolve(coords);
                    }else{
                        reject(coords["describe"]);
                    }
                }, 
                function(err){
                  reject(err);
                }
              );
          });
      else
          return new Promise(
              function(resolve, reject) {
                 this.geolocation.getCurrentPosition({timeout: 10000}).then((resp) => {
                    resolve(resp.coords);
                  }).catch((error) => {
                      let msg = '定位失败!';
                      if(error.code == error.PERMISSION_DENIED)
                          msg += "请确认本软件拥有定位权限";
                      else if(error.code == error.POSITION_UNAVAILABLE)
                          msg += "无法定位,请确认联网或打开定位功能";
                      else if(error.code == error.TIMEOUT)
                          msg += "定位超时,请重试...";
                      reject(msg);
                  });
              });
  }

cordova-qdc-baidu-location's People

Contributors

chenxinbin avatar mrwutong avatar

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.