百度智能小程序 打開(kāi)地圖選擇位置

2020-09-05 14:20 更新

swan.chooseLocation

解釋: 打開(kāi)地圖選擇位置。需要用戶授權(quán) scope.userLocation 。使用該 API 需通過(guò)獲取用戶授權(quán)設(shè)置申請(qǐng)授權(quán)后方可對(duì)用戶發(fā)起授權(quán)申請(qǐng),可在 swan.authorize 中查看相關(guān)錯(cuò)誤碼信息。

方法參數(shù)

Object object

object 參數(shù)說(shuō)明

屬性名類型必填默認(rèn)值說(shuō)明

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 返回參數(shù)說(shuō)明

參數(shù)參數(shù)類型說(shuō)明

name

String

位置名稱

address

String

詳細(xì)地址

latitude

Number

緯度,浮點(diǎn)數(shù),范圍為 -90~90 ,負(fù)數(shù)表示南緯。使用 gcj02 國(guó)測(cè)局坐標(biāo)系,查詢指定地點(diǎn)的經(jīng)緯度。

longitude

Number

經(jīng)度,浮點(diǎn)數(shù),范圍為 -180~180 ,負(fù)數(shù)表示西經(jīng)。使用 gcj02 國(guó)測(cè)局坐標(biāo)系,查詢指定地點(diǎn)的經(jīng)緯度。

示例 

在開(kāi)發(fā)者工具中打開(kāi)



圖片示例

代碼示例

<view class="wrap">
    <view class="card-area">
        <view class="list-area border-bottom">
            <view class="list-item-key-4">名稱</view>
            <view class="list-item-value">{{name}}</view>
        </view>
        <view class="list-area border-bottom">
            <view class="list-item-key-4">地址</view>
            <view class="list-item-value">{{address}}</view>
        </view>
        <view class="list-area border-bottom">
            <view class="list-item-key-4">坐標(biāo)</view>
            <view class="list-item-value">{{longitude}} {{latitude}}</view>
        </view>
        <button bindtap="chooseLocation" type="primary" hover-stop-propagation="true">點(diǎn)擊選擇位置</button>
    </view>
</view>
Page({
    data: {
        longitude: '',
        latitude: ''
    },
    chooseLocation() {
        swan.authorize({
            scope: 'scope.userLocation',
            success: res => {
                console.log('authorize', res);
            },
            fail: err => {
                swan.openSetting({});
            }
        });
        swan.chooseLocation({
            success: res => {
                console.log('chooseLocation success', res);
                let longitude = 'E:' + this.formatLocation(res.longitude) + '′';
                let latitude = 'N:' + this.formatLocation(res.latitude) + '′';
                console.log('longitude', longitude);
                console.log('latitude', latitude);
                this.setData({
                    name: res.name,
                    address: res.address,
                    longitude: longitude,
                    latitude: latitude
                });
            },
            fail: err => {
                console.log('錯(cuò)誤碼:' + err.errCode);
                console.log('錯(cuò)誤信息:' + err.errMsg);
            }
        });
    },
    formatLocation(data) {
        return data.toFixed(2).replace('.', '°');
    }
});

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

Q:如何查詢指定地點(diǎn)的經(jīng)緯度?

A:當(dāng)前我們提供以下方式可以查詢到您指定地點(diǎn)的經(jīng)緯度

  • 推薦使用 swan.chooseLocation 可獲取到選擇當(dāng)前位置的經(jīng)緯度。在開(kāi)發(fā)者工具中調(diào)用 swan.chooseLocation 用日志輸出看到經(jīng)緯度(坐標(biāo)系 GCJ02 ,可以直接用于小程序),具體方法可參考官方示例在開(kāi)發(fā)者工具中預(yù)覽效果。
  • 使用地圖拾取器百度地圖拾取器:坐標(biāo)系 BD09 ,不能直接用于小程序,需要開(kāi)發(fā)者轉(zhuǎn)換為 GCJ02 。后期小程序優(yōu)化后可以支持該坐標(biāo)系。騰訊地圖拾取器:坐標(biāo)系 GCJ02 ,可直接使用。高德地圖拾取器:坐標(biāo)系 GCJ02 ,可直接使用。


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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)