codecamp

百度智能小程序API 位置

获取位置


getLocation

解释: 获取当前的地理位置、速度。当用户离开智能小程序后,此接口无法调用。

参数: Object

Object 参数说明:

参数 类型 必填 说明
type String 默认为 wgs84 返回 gps 坐标,可选 gcj02
success Function 接口调用成功的回调函数,返回内容详见返回参数说明。
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success 返回参数说明:

参数 说明
latitude 纬度,浮点数,范围为-90~90,负数表示南纬
longitude 经度,浮点数,范围为-180~180,负数表示西经
speed 速度,浮点数,单位m/s
accuracy 位置的精确度
altitude 高度,单位 m
verticalAccuracy 垂直精度,单位 m(Android 无法获取,返回 0)
horizontalAccuracy 水平精度,单位 m

示例:

swan.getLocation({
    type: 'gcj02',
    success: function (res) {
        console.log("纬度:" + res.latitude);
        console.log("经度:" + res.longitude);
    },
    fail: function (err) {
        console.log("错误码:" + err.errCode);
        console.log("错误信息:" + err.errMsg);
    }
});

查看位置


openLocation

解释: 使用手百内置地图查看位置。

参数: Object​​Object 参数说明:

参数 类型 必填 说明
latitude Float 纬度,范围为 -90~90,负数表示南纬
longitude Float 经度,范围为 -180~180,负数表示西经
scale INT 缩放比例,范围 4~21,默认为16

示例:

swan.getLocation({  
    type: 'gcj02',
    success: function (res) {
        swan.openLocation({
            latitude: res.latitude,
            longitude: res.longitude,
            scale: 18
        })
    },
    fail: function (err) {
        console.log("错误码:" + err.errCode);
        console.log("错误信息:" + err.errMsg);
    }
});

地图组件控制


createMapContext

解释: 创建并返回 map 上下文 mapContext 对象。在自定义组件下,第二个参数传入组件实例 this,以操作组件内 <map/> 组件。mapContext 通过 mapId 跟一个 组件绑定,通过它可以操作对应的 组件。

参数: mapId

mapContext 对象的方法列表:

方法 参数 说明
getCenterLocation OBJECT 获取当前地图中心的经纬度,返回的是 gcj02 坐标系,可以用于 swan.openLocation
moveToLocation 将地图中心移动到当前定位点,需要配合 map 组件的 show-location 使用
translateMarker OBJECT 平移 marker,带动画
includePoints OBJECT 缩放视野展示所有经纬度
getRegion OBJECT 获取当前地图的视野范围
getScale OBJECT 获取当前地图的缩放级别

getCenterLocation 的 Object 参数列表:

参数名 类型 必填 说明
success Function 接口调用成功的回调函数 ,res = { longitude: “经度”, latitude: “纬度”}
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

translateMarker 的 Object 参数列表:

参数名 类型 必填 说明
markerId Number 指定 marker
destination Object 指定marker移动到的目标点
autoRotate Boolean 移动过程中是否自动旋转 marker
rotate Number marker 的旋转角度
duration duration 动画持续时长,默认值1000ms,平移与旋转分别计算
fail Function 接口调用失败的回调函数

includePoints 的 OBJECT 参数列表:

参数名 类型 必填 说明
points Array 要显示在可视区域内的坐标点列表,[{latitude, longitude}]
padding Array 坐标点形成的矩形边缘到地图边缘的距离,单位像素。格式为[上,右,下,左],安卓上只能识别数组第一项,上下左右的 padding 一致。开发者工具暂不支持 padding 参数。

getRegion 的 OBJECT 参数列表:

参数名 类型 必填 说明
success Function 接口调用成功的回调函数,res = {southwest, northeast},西南角与东北角的经纬度
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

getScale 的 OBJECT 参数列表:

参数名 类型 必填 说明
success Function 接口调用成功的回调函数,res = {scale}
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

示例:

<view class="wrap">
    <map id="myMap"
        style="width: 100%"
    </map>
	<button type="primary" bindtap="getCenterLocation">获取位置</button>
	<button type="primary" bindtap="moveToLocation">移动位置</button>
	<button type="primary" bindtap="translateMarker">移动标注</button>
	<button type="primary" bindtap="includePoints">缩放视野展示所有经纬度</button>
	<button type="primary" bindtap="getRegion">获取当前地图的视野范围</button>
</view>
Pages({
    data: {
        latitude: '40.042500',
        longitude: '116.274040',
    },
    onReady() {
        this.mapContext = swan.createMapContext('myMap');
    },
    getCenterLocation: function () {
        this.mapContext.getCenterLocation({
            success: function (res) {
                console.log("经度" + res.longitude);
                console.log("纬度" + res.latitude);
            }
        })
    },
    moveToLocation: function () {
        this.mapContext.moveToLocation();
    },
    translateMarker: function () {
        this.mapContext.translateMarker({
            markerId: 0,
            autoRotate: true,
            duration: 1000,
            destination: {
                latitude: 23.10229,
                longitude: 113.3345211,
            },
            animationEnd() {
                console.log('animation end');
            }
        })
    },
    includePoints: function () {
        this.mapContext.includePoints({
            padding: [10],
            points: [{
                latitude: 23,
                longitude: 113.33,
            }, {
                latitude: 23,
                longitude: 113.3345211,
            }]
        })
    },
    getRegion: function () {
        this.mapContext.getRegion({
            success: function (res) {
                console.log("西南角的经纬度" + res.southwest);
                console.log("东北角的经纬度" + res.northeast);
            }
        })
    }
});
百度智能小程序API 数据存储
百度智能小程序API 绘图
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

百度智能小程序开发文档

百度智能小程序 组件

百度智能小程序 地图

百度智能小程序 画布

百度智能小程序 API

百度智能小程序 界面

百度智能小程序 关注小程序引导组件

百度智能小程序 自定义组件

百度智能小程序 媒体

百度智能小程序 设备

百度智能小程序 拨打电话

百度智能小程序 内存警报

百度智能小程序 手机联系人

百度智能小程序 用户截屏事件

百度智能小程序 第三方平台

百度智能小程序 开放接口

百度智能小程序 百度收银支付

百度智能小程序 分包预下载

百度智能小程序 数据分析

百度智能小程序 服务端

百度智能小程序 云开发

百度智能小程序 初始化

百度智能小程序 云函数

百度智能小程序 服务端初始化

百度智能小程序 服务器获取上下文

百度智能小程序 服务端云函数

百度智能小程序 开发教程

百度智能小程序 功能开发

百度智能小程序 基本原理

百度智能小程序 小程序自动化

百度智能小程序 视频教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }