支付寶小程序API 位置

2020-09-15 10:53 更新

my.chooseLocation

簡(jiǎn)介

my.chooseLocation 是使用支付寶內(nèi)置地圖選擇地理位置的 API。

使用限制

  • 暫無(wú)境外地圖數(shù)據(jù),在中國(guó)內(nèi)地(不含港澳臺(tái))以外的地區(qū)可能無(wú)法正常調(diào)用此 API。
  • 僅支持高德地圖 style 與火星坐標(biāo)系。

掃碼體驗(yàn)

mychooselocation.png

效果示例

選擇地理位置.gif

示例代碼

  1. // API-DEMO page/API/choose-location/choose-location.json
  2. {
  3. "defaultTitle": "選擇位置"
  4. }
  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>經(jīng)度:</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>位置名稱(chēng):</text>
  14. <input value="{{name}}"></input>
  15. </view>
  16. <view class="page-section-demo">
  17. <text>詳細(xì)位置:</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>
  1. // API-DEMO page/API/choose-location/choose-location.js
  2. Page({
  3. data: {
  4. longitude: '120.126293',
  5. latitude: '30.274653',
  6. name: '黃龍萬(wàn)科中心',
  7. address: '學(xué)院路77號(hào)',
  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: '調(diào)用失敗:'+JSON.stringify(error), });
  23. },
  24. });
  25. },
  26. })
  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. }

入?yún)?/h4>

Object 類(lèi)型,屬性如下:

屬性 類(lèi)型 必填 描述
success Function 調(diào)用成功的回調(diào)函數(shù)。
fail Function 調(diào)用失敗的回調(diào)函數(shù)。
complete Function 調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)。

success 回調(diào)函數(shù)
屬性 類(lèi)型 描述
name String 位置名稱(chēng)。
address String 詳細(xì)地址。
latitude Number 緯度,浮點(diǎn)數(shù),范圍為-90~90,負(fù)數(shù)表示南緯。
longitude Number 經(jīng)度,浮點(diǎn)數(shù),范圍為-180~180,負(fù)數(shù)表示西經(jīng)。
provinceName String 省份名稱(chēng)。
cityName String 城市名稱(chēng)。

my.getLocation

簡(jiǎn)介

my.getLocation 是獲取用戶(hù)當(dāng)前的地理位置信息的 API。

使用限制

  • 基礎(chǔ)庫(kù) 1.1.1 或更高版本。若版本較低,建議采取 兼容處理
  • 暫無(wú)境外地圖數(shù)據(jù),在中國(guó)內(nèi)地(不含港澳臺(tái))以外的地區(qū)可能無(wú)法正常調(diào)用此 API。
  • 僅支持高德地圖 style 與火星坐標(biāo)系。

掃碼體驗(yàn)

獲取用戶(hù)當(dāng)前的地理位置信息.jpeg

效果示例

獲取用戶(hù)當(dāng)前的地理位置.gif

示例代碼

  1. // API-DEMO page/API/get-location/get-location.json
  2. {
  3. "defaultTitle": "獲取位置"
  4. }
  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>當(dāng)前位置經(jīng)緯度</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>
  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
  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. })
  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. }

入?yún)?/h4>

Object 類(lèi)型,屬性如下:

屬性 類(lèi)型 必填 描述
cacheTimeout Number 支付寶客戶(hù)端經(jīng)緯度定位緩存過(guò)期時(shí)間,單位為秒。默認(rèn) 30 秒(s)。使用緩存會(huì)加快定位速度,緩存過(guò)期會(huì)重新定位。
type Number 獲取經(jīng)緯度數(shù)據(jù)的類(lèi)型。默認(rèn)值為 0。最低基礎(chǔ)庫(kù)版本限制為 1.1.1。0:獲取經(jīng)緯度。1:獲取經(jīng)緯度和詳細(xì)到區(qū)縣級(jí)別的逆地理編碼數(shù)據(jù)。2:獲取經(jīng)緯度和詳細(xì)到街道級(jí)別的逆地理編碼數(shù)據(jù),不推薦使用。(不推薦使用的原因:精度過(guò)高,接口返回的速度會(huì)變慢。)3:獲取經(jīng)緯度和詳細(xì)到POI級(jí)別的逆地理編碼數(shù)據(jù),不推薦使用。(不推薦使用的原因:精度過(guò)高,接口返回的速度會(huì)變慢。)
success Function 調(diào)用成功的回調(diào)函數(shù)。
fail Function 調(diào)用失敗的回調(diào)函數(shù)。
complete Function 調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)。

success 回調(diào)函數(shù)
屬性 類(lèi)型 描述
longitude String 經(jīng)度
latitude String 緯度
accuracy String 精確度,單位米 (m)。
horizontalAccuracy String 水平精確度,單位為米 (m)。
country String 國(guó)家(type>0生效)。最低基礎(chǔ)庫(kù)版本限制為 1.1.1。
countryCode String 國(guó)家編號(hào) (type>0生效)。最低基礎(chǔ)庫(kù)版本限制為 1.1.1。
province String 省份(type>0生效)。最低基礎(chǔ)庫(kù)版本限制為 1.1.1。
city String 城市(type>0生效)。最低基礎(chǔ)庫(kù)版本限制為 1.1.1。
cityAdcode String 城市級(jí)別的地區(qū)代碼(type>0生效)。最低基礎(chǔ)庫(kù)版本限制為 1.1.1。
district String 區(qū)縣(type>0生效)。最低基礎(chǔ)庫(kù)版本限制為 1.1.1。
districtAdcode String 區(qū)縣級(jí)別的地區(qū)代碼(type>0生效)。最低基礎(chǔ)庫(kù)版本限制為 1.1.1。
streetNumber Object 需要街道級(jí)別逆地理的才會(huì)有的字段,街道門(mén)牌信息,結(jié)構(gòu)是:{street, number} (type>1生效)。最低基礎(chǔ)庫(kù)版本限制為 1.1.1。
pois Array 需要 POI (Point of Interest,興趣點(diǎn),在地理信息系統(tǒng)中,一個(gè) POI 可以是一棟房子、一個(gè)商鋪、一個(gè)郵筒、一個(gè)公交站等)級(jí)別的地理位置才會(huì)有的字段,定位點(diǎn)附近的 POI 信息,結(jié)構(gòu)是:{name, address}(type>2生效)。最低基礎(chǔ)庫(kù)版本限制為 1.1.1。

錯(cuò)誤碼

錯(cuò)誤碼 描述 解決方案
11 請(qǐng)確認(rèn)定位相關(guān)權(quán)限已開(kāi)啟。 提示用戶(hù)確認(rèn)手機(jī)是否已給支付寶 APP 獲取定位權(quán)限。
12 網(wǎng)絡(luò)異常,請(qǐng)稍后再試。 提示用戶(hù)檢查當(dāng)前網(wǎng)絡(luò)。
13 定位失敗,請(qǐng)稍后再試。 提示用戶(hù)再次嘗試。
14 業(yè)務(wù)定位超時(shí)。 提示用戶(hù)再次嘗試。
2001 用戶(hù)拒絕給小程序授權(quán)。 提示用戶(hù)接受小程序授權(quán)。

常見(jiàn)問(wèn)題 FAQ

Q:my.getLocation 第一次允許授權(quán)后刪除小程序應(yīng)用,重新打開(kāi)會(huì)需要重新授權(quán)嗎?

A:需要重新授權(quán),刪除小程序應(yīng)用后會(huì)將獲取定位的授權(quán)關(guān)系一起刪除。

my.openLocation

簡(jiǎn)介

my.openLocation 是使用支付寶內(nèi)置地圖查看位置的 API。

使用限制

  • 暫無(wú)境外地圖數(shù)據(jù),在中國(guó)內(nèi)地(不含港澳臺(tái))以外的地區(qū)可能無(wú)法正常調(diào)用此 API。
  • 僅支持高德地圖 style 與火星坐標(biāo)系。

掃碼體驗(yàn)

支付寶查看位置.jpeg

效果示例

openlocation.gif

示例代碼

  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>經(jīng)度</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>位置名稱(chēng)</text>
  14. <input type="text" disabled="{{true}}" value="{{name}}" name="name"></input>
  15. </view>
  16. <view class="page-section-demo">
  17. <text>詳細(xì)位置</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: '黃龍萬(wàn)科中心',
  7. address: '學(xué)院路77號(hào)',
  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. })

入?yún)?/h4>

Object 類(lèi)型,屬性如下:

屬性 類(lèi)型 必填 描述
longitude String 經(jīng)度。
latitude String 緯度。
name String 位置名稱(chēng)。
address String 地址的詳細(xì)說(shuō)明。
scale Number 縮放比例,范圍 3~19,默認(rèn)為 15。
success Function 調(diào)用成功的回調(diào)函數(shù)。
fail Function 調(diào)用失敗的回調(diào)函數(shù)。
complete Function 調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)