文档

位置

更新时间:

my.chooseLocation

该接口用于使用内置地图选择地理位置。

  • 在 Android 客户端使用此 API 时,需要将申请获得的高德 key 加到 AndroidManifest,详情参见 申请高德 Key
  • 在 iOS 端使用此 API 时,需要在 beforeDidFinishLaunchingWithOptions 方法中设置高德定位的 key,所需代码如下所示。请参考 获取 Key 文档以获得高德定位的 Key。
    1. [APMapKeySetting getInstance].apiKey = @"高德定位的 Key"

使用限制

  • 暂无境外地图数据,在中国内地(不含港澳台)以外的地区可能无法正常调用此 API。
  • 仅支持高德地图 Style 与火星坐标系。

效果示例

chooselocation

入参

Object 类型,属性如下:

属性 类型 必填 描述
success Function 调用成功的回调函数。
fail Function 调用失败的回调函数。
complete Function 调用结束的回调函数(调用成功、失败都会执行)。

success 回调函数

属性 类型 描述
name String 位置名称。
address String 详细地址。
latitude Number 纬度,浮点数,范围为 -90 ~ 90,负数表示南纬。
longitude Number 经度,浮点数,范围为 -180 ~ 180,负数表示西经。
provinceName String 省份名称。
cityName String 城市名称。

代码示例

.json 代码示例:

  1. // API-DEMO page/API/choose-location/choose-location.json
  2. {
  3. "defaultTitle": "选择位置"
  4. }

.axml 代码示例:

  1. <!-- API-DEMO page/API/choose-location/choose-location.axml-->
  2. <view class="page">
  3. <view class="page-section">
  4. <view class="page-section-demo">
  5. <text>经度:</text>
  6. <input value="{{longitude}}"></input>
  7. </view>
  8. <view class="page-section-demo">
  9. <text>纬度:</text>
  10. <input value="{{latitude}}"></input>
  11. </view>
  12. <view class="page-section-demo">
  13. <text>位置名称:</text>
  14. <input value="{{name}}"></input>
  15. </view>
  16. <view class="page-section-demo">
  17. <text>详细位置:</text>
  18. <input value="{{address}}"></input>
  19. </view>
  20. <view class="page-section-btns">
  21. <view onTap="chooseLocation">选择位置</view>
  22. </view>
  23. </view>
  24. </view>

.js 代码示例:

  1. // API-DEMO page/API/choose-location/choose-location.js
  2. Page({
  3. data: {
  4. longitude: '120.126293',
  5. latitude: '30.274653',
  6. name: '黄龙万科中心',
  7. address: '学院路77号',
  8. },
  9. chooseLocation() {
  10. var that = this
  11. my.chooseLocation({
  12. success:(res)=>{
  13. console.log(JSON.stringify(res))
  14. that.setData({
  15. longitude:res.longitude,
  16. latitude:res.latitude,
  17. name:res.name,
  18. address:res.address
  19. })
  20. },
  21. fail:(error)=>{
  22. my.alert({content: '调用失败:'+JSON.stringify(error), });
  23. },
  24. });
  25. },
  26. })

.acss 代码示例:

  1. /* API-DEMO page/API/choose-location/choose-location.acss */
  2. .page-body-info {
  3. height: 250rpx;
  4. }
  5. .page-body-text-location {
  6. display: flex;
  7. font-size: 50rpx;
  8. }
  9. .page-body-text-location text {
  10. margin: 10rpx;
  11. }
  12. .page-section-location-text{
  13. color: #49a9ee;
  14. }

my.getLocation

该接口用于获取用户当前的地理位置信息。

  • 在 Android 客户端使用此 API 时,需要将申请获得的高德 key 加到 AndroidManifest,详情参见 申请高德 Key
  • 在 iOS 端使用此 API 时,需要在 beforeDidFinishLaunchingWithOptions 方法中设置高德定位的 key,所需代码如下所示。请参考 获取 Key 文档以获得高德定位的 Key。

    1. [LBSmPaaSAdaptor sharedInstance].shouldAMapRegeoWhenLBSFailed = YES;
    2. [AMapServices sharedServices].apiKey = @"高德定位的 Key"

使用限制

  • 基础库 1.1.1 及以上版本支持该接口,低版本需做兼容处理,操作参见 小程序基础库说明
  • 暂无境外地图数据,在中国内地(不含港澳台)以外的地区可能无法正常调用此 API。
  • 仅支持高德地图 Style 与火星坐标系。

效果示例

getLocation

入参

Object 类型,属性如下:

名称
类型
必填
描述
cacheTimeout
Number
mPaaS 客户端经纬度定位缓存过期时间,单位为秒。默认 30 秒(s)。使用缓存会加快定位速度,缓存过期会重新定位。
type
Number
获取经纬度数据的类型。默认值为 0。最低基础库版本限制为 1.1.1
  • 0:获取经纬度。
  • 1:获取经纬度和详细到区县级别的逆地理编码数据。
  • 2:获取经纬度和详细到街道级别的逆地理编码数据,不推荐使用。(不推荐使用的原因:精度过高,接口返回的速度会变慢。)
  • 3:获取经纬度和详细到 POI 级别的逆地理编码数据,不推荐使用。(不推荐使用的原因:精度过高,接口返回的速度会变慢。)
success
Function
调用成功的回调函数。
fail
Function
调用失败的回调函数。
complete
Function
调用结束的回调函数(调用成功、失败都会执行)。

success 回调函数

名称
类型
描述
最低版本
longitude
String
经度
-
latitude
String
纬度
-
accuracy
String
精确度,单位米(m)。
-
horizontalAccuracy
String
水平精确度,单位米(m)。
-
country
String
国家(type>0 生效)
countryCode
String
国家编号(type>0 生效)
province
String
省份(type>0 生效)
city
String
城市(type>0 生效)
cityAdcode
String
城市级别的地区代码(type>0 生效)
district
String
区县(type>0 生效)
districtAdcode
String
区县级别的地区代码(type>0 生效)
streetNumber
Object
需要街道级别逆地理编码数据时,才会返回该字段。街道门牌信息,结构是:{street, number} (type>1 生效)
pois
array
需要 POI 级别逆地理编码数据时,才会返回该字段。定位点附近的 POI 信息,结构是:{name, address}(type>2 生效)

fail 回调函数

Object 类型,属性如下:

属性 类型 描述
error String 错误码。
errorMessage String 错误信息。

代码示例

.json 代码示例:

  1. // API-DEMO page/API/get-location/get-location.json
  2. {
  3. "defaultTitle": "获取位置"
  4. }

.axml 代码示例:

  1. <!-- API-DEMO page/API/get-location/get-location.axml-->
  2. <view class="page">
  3. <view class="page-section">
  4. <view class="page-section-demo">
  5. <view>当前位置经纬度</view>
  6. <block a:if="{{hasLocation === false}}">
  7. <text>未获取</text>
  8. </block>
  9. <block a:if="{{hasLocation === true}}">
  10. <view class="page-body-text-location">
  11. <text>E{{location.longitude[0]}}°{{location.longitude[1]}}′</text>
  12. <text>N{{location.latitude[0]}}°{{location.latitude[1]}}′</text>
  13. </view>
  14. </block>
  15. </view>
  16. <view class="page-section-btns">
  17. <view onTap="getLocation">获取位置</view>
  18. <view onTap="clear">清空</view>
  19. </view>
  20. </view>
  21. </view>

.js 代码示例:

  1. // API-DEMO page/API/get-location/format-location.js
  2. function formatLocation(longitude, latitude) {
  3. longitude = Number(longitude).toFixed(2),
  4. latitude = Number(latitude).toFixed(2)
  5. return {
  6. longitude: longitude.toString().split('.'),
  7. latitude: latitude.toString().split('.')
  8. }
  9. }
  10. export default formatLocation

.js 代码示例:

  1. // API-DEMO page/API/get-location/get-location.js
  2. import formatLocation from './format-location.js';
  3. Page({
  4. data: {
  5. hasLocation: false,
  6. },
  7. getLocation() {
  8. var that = this;
  9. my.showLoading();
  10. my.getLocation({
  11. success(res) {
  12. my.hideLoading();
  13. console.log(res)
  14. that.setData({
  15. hasLocation: true,
  16. location: formatLocation(res.longitude, res.latitude)
  17. })
  18. },
  19. fail() {
  20. my.hideLoading();
  21. my.alert({ title: '定位失败' });
  22. },
  23. })
  24. },
  25. clear() {
  26. this.setData({
  27. hasLocation: false
  28. })
  29. }
  30. })

.acss 代码示例:

  1. /* API-DEMO page/API/get-location/get-location.acss */
  2. .page-body-info {
  3. height: 250rpx;
  4. }
  5. .page-body-text-small {
  6. font-size: 24rpx;
  7. color: #000;
  8. margin-bottom: 100rpx;
  9. }
  10. .page-body-text-location {
  11. display: flex;
  12. font-size: 50rpx;
  13. }
  14. .page-body-text-location text {
  15. margin: 10rpx;
  16. }

错误码

错误码 描述 解决方案
11 请确认定位相关权限已开启。 提示用户确认手机是否已给 App 授予获取定位权限。
12 网络异常,请稍后再试。 提示用户检查当前网络。
13 定位失败,请稍后再试。 提示用户再次尝试。
14 业务定位超时。 提示用户再次尝试。
2001 用户拒绝给小程序授权。 提示用户接受小程序授权。

常见问题

  • Q:my.getLocation 第一次允许授权后删除小程序应用,重新打开会需要重新授权吗?
    A:需要重新授权,删除小程序应用后会将获取定位的授权关系一起删除。

my.openLocation

该接口用于使用 mPaaS 小程序内置地图查看位置。

使用限制

  • 暂无境外地图数据,在中国内地(不含港澳台)以外的地区可能无法正常调用此 API。
  • 仅支持高德地图 Style 与火星坐标系。

效果示例

my.openLocation

入参

名称 类型 必填 描述
longitude String 经度。
latitude String 纬度。
name String 位置名称。
address String 地址的详细说明。
scale Number 缩放比例,范围 3~19,默认为 15。
success Function 调用成功的回调函数。
fail Function 调用失败的回调函数。
complete Function 调用结束的回调函数(调用成功、失败都会执行)。

代码示例

  1. // API-DEMO page/API/open-location/open-location.json
  2. {
  3. "defaultTitle": "查看位置"
  4. }
  1. <!-- API-DEMO page/API/open-location/open-location.axml-->
  2. <view class="page">
  3. <view class="page-section">
  4. <view class="page-section-demo">
  5. <text>经度</text>
  6. <input type="text" disabled="{{true}}" value="{{longitude}}" name="longitude"></input>
  7. </view>
  8. <view class="page-section-demo">
  9. <text>纬度</text>
  10. <input type="text" disabled="{{true}}" value="{{latitude}}" name="latitude"></input>
  11. </view>
  12. <view class="page-section-demo">
  13. <text>位置名称</text>
  14. <input type="text" disabled="{{true}}" value="{{name}}" name="name"></input>
  15. </view>
  16. <view class="page-section-demo">
  17. <text>详细位置</text>
  18. <input type="text" disabled="{{true}}" value="{{address}}" name="address"></input>
  19. </view>
  20. <view class="page-section-btns">
  21. <view type="primary" formType="submit" onTap="openLocation">查看位置</view>
  22. </view>
  23. </view>
  24. </view>
  1. // API-DEMO page/API/open-location/open-location.js
  2. Page({
  3. data: {
  4. longitude: '120.126293',
  5. latitude: '30.274653',
  6. name: '黄龙万科中心',
  7. address: '学院路77号',
  8. },
  9. openLocation() {
  10. my.openLocation({
  11. longitude: this.data.longitude,
  12. latitude: this.data.latitude,
  13. name: this.data.name,
  14. address: this.data.address,
  15. })
  16. }
  17. })
  • 本页导读 (0)
文档反馈