本帖最后由 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 代码:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
- <title>title</title>
- <link rel="stylesheet" type="text/css" href="../css/api.css"/>
- <style>
- body{
- text-align: center;
- }
- button{
- width: 100%;
- height: 70px;
- line-height: 70px;
- background-color: #00ccff;
- color: #fff;
- text-align: center;
- margin-top:20px;
- border-radius: 8px;
- }
- #mapInfo{
- width: 100%;
- text-align: center;
- }
- header{
- width: 100%;
- line-height: 105px;
- height: 80px;
- background-color: green;
- text-align: center;
- color: #fff;
- }
- </style>
- </head>
- <body>
- <header>
- bmLocation-demo
- </header>
- <div id="mapInfo"></div>
- <button type="button" name="button" onclick="getPermissionState()">获取授权验证码</button>
- <button type="button" name="button" onclick="configManager()">配置定位参数</button>
- <button type="button" name="button" onclick="singleLocation()">单次定位</button>
- <button type="button" name="button" onclick="StartBmap()">连续定位</button>
- <button type="button" name="button" onclick="stopLocation()">停止定位</button>
- <button type="button" name="button" onclick="trans()">经纬度坐标转换</button>
- <button type="button" name="button" onclick="judge()">是否在中国大陆</button>
- </body>
- <script type="text/javascript" src="../script/api.js"></script>
- <script type="text/javascript">
- var mapInfo = $api.byId('mapInfo');
- apiready = function(){
- };
- //连续定位开启
- function StartBmap(){
- $api.html(mapInfo,'定位开始');
- var num = 0;//次数
- var bmLocation = api.require('bmLocation');
- bmLocation.start({
- locatingWithReGeocode: true,
- backgroundLocation: false
- }, function(ret) {
- var sta = ret.status;
- if (sta) {
- var lat = ret.location.latitude;
- var lon = ret.location.longitude;
- var t = ret.timestamp;
- var str = '经度:' + lon + '<br>';
- str += '纬度:' + lat + '<br>';
- num += 1;
- $api.html(mapInfo ,'第'+ num + '次' + str )
- } else {
- api.alert({ msg: '发生错误' });
- }
- });
- }
- //停止定位
- function stopLocation(){
- var bmLocation = api.require('bmLocation');
- bmLocation.stopLocation();
- $api.html(mapInfo, '已停止');
- }
- //获取授权码
- function getPermissionState() {
- var bmLocation = api.require('bmLocation');
- bmLocation.getPermissionState(function(ret) {
- var sta = ret.code;
- $api.html(mapInfo, '授权码:' + sta);
- });
- }
- //配置 参数
- function configManager() {
- var bmLocation = api.require('bmLocation');
- bmLocation.configManager({
- accuracy: 'device_sensors',
- filter: 1,
- activityType:'automotiveNavigation',
- coordinateType:'GCJ02',
- locationTimeout: 10,
- reGeocodeTimeout: 10
- });
- $api.html(mapInfo, '已配置' );
- }
- //单次定位
- function singleLocation() {
- var bmLocation = api.require('bmLocation');
- bmLocation.singleLocation({
- reGeocode: true,
- netWorkState: false
- }, function(ret) {
- var sta = ret.status;
- if (sta) {
- var lat = ret.location.latitude;
- var lon = ret.location.longitude;
- var t = ret.timestamp;
- var str = '经度:' + lon + '<br>';
- str += '纬度:' + lat + '<br>';
- $api.html(mapInfo, '单次' + str );
- } else {
- $api.html(mapInfo, '单次错误');
- }
- });
- }
- //坐标格式转换
- function trans() {
- var bmLocation = api.require('bmLocation');
- bmLocation.trans({
- latitude: 116,
- latitude: 39,
- desType:'BMK09LL',
- srcType:'GCJ02'
- }, function(ret) {
- var sta = ret.status;
- if (sta) {
- var lat = ret.location.latitude;
- var lon = ret.location.longitude;
- var t = ret.timestamp;
- var str = '经度:' + lon + '<br>';
- str += '纬度:' + lat + '<br>';
- $api.html(mapInfo, '格式转换' + str);
- } else {
- api.alert({ msg: '发生错误' });
- }
- });
- }
- //判断是否在国内
- function judge() {
- var bmLocation = api.require('bmLocation');
- bmLocation.trans({
- latitude: 116,
- latitude: 39,
- coordinateType:'BMK09LL'
- }, function(ret) {
- var sta = ret.status;
- if (sta) {
- $api.html(mapInfo, '在');
- //api.alert({ msg: '在' });
- } else {
- api.alert({ msg: '不在' });
- }
- });
- }
- </script>
- </html>
复制代码
编译时需要在云编译页面勾选定位权限
如遇bug 或者问题 欢迎提出。
|