帖子
帖子
用户
博客
课程
12下一页
返回列表 发新帖
显示全部楼层
20
帖子
0
勋章
6613
Y币

bmLocation 模块使用分享。

[复制链接]
发表于 2019-9-2 14:14:09
本帖最后由 yxWin 于 2019-9-2 14:16 编辑

bmLocation
bmLocation 封装了百度地图定位的 SDK,是 baiduLocation 模块的优化升级版。百度地图移动端定位SDK,是百度地图开放平台面向广大移动应用开发者全新推出的一款定位服务产品。开发者在自己的移动应用中集成定位SDK,可轻松实现获取当前位置信息的功能。
如果还在使用baiduLocation的朋友可以替换一下。因为sdk升级了,baiduLocation 已停止维护了。

说一下用法
首先需要去百度地图开放平台注册登录,然后创建自己的项目。
建议先看一下文档http://lbsyun**.**/index.php?title=ios-locsdk/guide/create-project/key(最下方有大图哦)

然后创建项目  Android 与ios  是分开创建的
ios 只需填入 包名即可 , Android 需要包名sha1

     

切记要检查一遍再确认,一定一定一定不要填错,否则会有无数的坑等着你跳
配置后提交去我的应用里>>>查看应用 会有你的应用的具体信息



这个时候就可以去配置config.xml文件了 按照文档把平台的key填入到项目

使用此模块之前 iOS 必须先配置 config 文件,配置方法如下:
  • 名称:bmLocation
  • 参数:apiKey
  • 配置示例:
  <feature name="bmLocation">    <param name="ios_api_key" value="81qz3dBYB5q2nGji4IYrawr1" />  </feature>
使用此模块之前 android 必须先配置 config 文件,配置方法如下:
  • 配置示例:
<meta-data     name="com.baidu.lbsapi.API_KEY"     value="您的百度ak" />
还需要配置后台的定位功能
后台定位功能:
本模块支持后台定位功能。由于系统限制,在 iOS 和 android 平台上表现有所不同。在 iOS 平台上,定位功能必须在 app 为当前激活使用状态,或者后台运行状态时。若将 app 从后台杀死,则本 app 的所有功能都被关闭,包括后台定位功能。若要集成后台定位功能,必须得配置 config 文件,然后云编译或自定义 loader 方可。配置实例如下:
//配置一个后台定位功能:<preference name="backgroundMode" value="location"/>//配置多个后台运行功能,各值之间用竖线 | 隔开:<preference name="backgroundMode" value="location | audio"/>
以上配置都成功后要先同步代码,然后在编译,就可以使用了
看一下demo 的运行截图


demo 代码:

  1. <!DOCTYPE html>
  2.   <html>
  3.   <head>
  4.       <meta charset="utf-8">
  5.       <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
  6.       <title>title</title>
  7.       <link rel="stylesheet" type="text/css" href="../css/api.css"/>
  8.       <style>
  9.           body{
  10.             text-align: center;
  11.           }
  12.           button{
  13.             width: 100%;
  14.             height: 70px;
  15.             line-height: 70px;
  16.             background-color: #00ccff;
  17.             color: #fff;
  18.             text-align: center;
  19.             margin-top:20px;
  20.             border-radius: 8px;
  21.             }
  22.             #mapInfo{
  23.               width: 100%;

  24.               text-align: center;
  25.             }
  26.             header{
  27.               width: 100%;
  28.               line-height: 105px;
  29.               height: 80px;
  30.               background-color: green;
  31.               text-align: center;
  32.               color: #fff;
  33.             }
  34.       </style>
  35.   </head>
  36.   <body>
  37.     <header>
  38.       bmLocation-demo
  39.     </header>
  40.     <div id="mapInfo"></div>
  41.         <button type="button" name="button" onclick="getPermissionState()">获取授权验证码</button>
  42.         <button type="button" name="button" onclick="configManager()">配置定位参数</button>
  43.         <button type="button" name="button" onclick="singleLocation()">单次定位</button>
  44.         <button type="button" name="button" onclick="StartBmap()">连续定位</button>
  45.         <button type="button" name="button" onclick="stopLocation()">停止定位</button>
  46.         <button type="button" name="button" onclick="trans()">经纬度坐标转换</button>
  47.         <button type="button" name="button" onclick="judge()">是否在中国大陆</button>
  48.   </body>
  49.   <script type="text/javascript" src="../script/api.js"></script>
  50.   <script type="text/javascript">
  51.       var mapInfo = $api.byId('mapInfo');
  52.       apiready = function(){

  53.       };
  54.       //连续定位开启
  55.       function StartBmap(){
  56.         $api.html(mapInfo,'定位开始');
  57.         var num = 0;//次数
  58.         var bmLocation = api.require('bmLocation');
  59.         bmLocation.start({
  60.             locatingWithReGeocode: true,
  61.             backgroundLocation: false
  62.         }, function(ret) {
  63.             var sta = ret.status;
  64.             if (sta) {
  65.                 var lat = ret.location.latitude;
  66.                 var lon = ret.location.longitude;
  67.                 var t = ret.timestamp;
  68.                 var str = '经度:' + lon + '<br>';
  69.                 str += '纬度:' + lat + '<br>';
  70.                 num += 1;
  71.                 $api.html(mapInfo ,'第'+ num + '次' + str )
  72.             } else {
  73.                 api.alert({ msg: '发生错误' });
  74.             }
  75.         });
  76.       }
  77.       //停止定位
  78.       function stopLocation(){
  79.         var bmLocation = api.require('bmLocation');
  80.         bmLocation.stopLocation();
  81.         $api.html(mapInfo, '已停止');
  82.       }
  83.       //获取授权码
  84.       function getPermissionState() {
  85.         var bmLocation = api.require('bmLocation');
  86.         bmLocation.getPermissionState(function(ret) {
  87.             var sta = ret.code;
  88.             $api.html(mapInfo, '授权码:' + sta);
  89.         });
  90.       }
  91.       //配置 参数
  92.       function configManager() {
  93.         var bmLocation = api.require('bmLocation');
  94.         bmLocation.configManager({
  95.             accuracy: 'device_sensors',
  96.             filter: 1,
  97.             activityType:'automotiveNavigation',
  98.             coordinateType:'GCJ02',
  99.             locationTimeout: 10,
  100.             reGeocodeTimeout: 10
  101.         });
  102.         $api.html(mapInfo, '已配置' );
  103.       }
  104.       //单次定位
  105.       function singleLocation() {
  106.         var bmLocation = api.require('bmLocation');
  107.         bmLocation.singleLocation({
  108.             reGeocode: true,
  109.             netWorkState: false
  110.         }, function(ret) {
  111.             var sta = ret.status;
  112.             if (sta) {
  113.                 var lat = ret.location.latitude;
  114.                 var lon = ret.location.longitude;
  115.                 var t = ret.timestamp;
  116.                 var str = '经度:' + lon + '<br>';
  117.                 str += '纬度:' + lat + '<br>';
  118.                 $api.html(mapInfo, '单次' + str );
  119.             } else {
  120.                 $api.html(mapInfo, '单次错误');
  121.             }
  122.         });
  123.       }
  124.       //坐标格式转换
  125.       function trans() {
  126.         var bmLocation = api.require('bmLocation');
  127.         bmLocation.trans({
  128.             latitude: 116,
  129.             latitude: 39,
  130.             desType:'BMK09LL',
  131.             srcType:'GCJ02'
  132.         }, function(ret) {
  133.             var sta = ret.status;
  134.             if (sta) {
  135.                 var lat = ret.location.latitude;
  136.                 var lon = ret.location.longitude;
  137.                 var t = ret.timestamp;
  138.                 var str = '经度:' + lon + '<br>';
  139.                 str += '纬度:' + lat + '<br>';
  140.                 $api.html(mapInfo, '格式转换' + str);

  141.             } else {
  142.                 api.alert({ msg: '发生错误' });
  143.             }
  144.         });
  145.       }

  146.       //判断是否在国内
  147.       function judge() {
  148.         var bmLocation = api.require('bmLocation');
  149.         bmLocation.trans({
  150.             latitude: 116,
  151.             latitude: 39,
  152.             coordinateType:'BMK09LL'
  153.         }, function(ret) {
  154.             var sta = ret.status;
  155.             if (sta) {
  156.               $api.html(mapInfo, '在');
  157.                 //api.alert({ msg: '在'  });
  158.             } else {
  159.                 api.alert({ msg: '不在' });
  160.             }
  161.         });
  162.       }
  163.   </script>
  164.   </html>
复制代码


编译时需要在云编译页面勾选定位权限


如遇bug 或者问题 欢迎提出。

本帖子中包含更多资源,您需要 登录 才可以下载或查看,没有帐号?立即注册

X
4
帖子
0
勋章
718
Y币
请问连续定位和单词定位的区别在哪里? 如果使用连续定位, 系统会以什么频度去定位? 回调函数的触发条件是什么?
谢谢
20
帖子
0
勋章
6613
Y币
hc.xiao 发表于 2020-3-3 08:24
请问连续定位和单词定位的区别在哪里? 如果使用连续定位, 系统会以什么频度去定位? 回调函数的触发条件 ...

区别就是 是否连续  频率不会太快的  如果是连续定位  每次都会有回调,只要开始定位就有回调的
4
帖子
0
勋章
718
Y币
多谢解读, 那么连续定位的频率是否可以控制呢?
另外, 有没有根据位置坐标能显示地图的模块?
20
帖子
0
勋章
6613
Y币
hc.xiao 发表于 2020-3-3 14:26
多谢解读, 那么连续定位的频率是否可以控制呢?
另外, 有没有根据位置坐标能显示地图的模块? ...

bmap  模块 看一下
20
帖子
0
勋章
6613
Y币
hc.xiao 发表于 2020-3-3 14:26
多谢解读, 那么连续定位的频率是否可以控制呢?
另外, 有没有根据位置坐标能显示地图的模块? ...

抱歉 频率不能控制
6
帖子
0
勋章
104
Y币
连续定位感觉没有起作用,刷新页面时只回调了一次,后面都没有回调了
20
帖子
0
勋章
6613
Y币
tanlin 发表于 2020-3-18 01:10
连续定位感觉没有起作用,刷新页面时只回调了一次,后面都没有回调了

刷新页面?
0
帖子
0
勋章
2
Y币
无网络情况下,bmLocation.singleLocation不会进callback
var bmLocation = api.require('bmLocation');
                    bmLocation.configManager({
                        coordinateType: 'BMK09LL'
                    });
                    bmLocation.singleLocation({
                        reGeocode: false,
                        netWorkState: false
                    }, function(ret) {
                        console.log(JSON.stringify(ret));
                        if (ret.status) {
                            console.log(ret.location.longitude);
                            console.log(ret.location.latitude);
                            alert(ret.location.longitude + '---' + ret.location.latitude);
                        } else {
                            mytoast('获取位置信息失败,请检查是否开启权限。');
                        }
                    });
20
帖子
0
勋章
6613
Y币
配置   云编译的定位权限    手机的定位权限  都检一下  都正确就没问题
12下一页
您需要登录后才可以回帖 登录

本版积分规则