百度智能小程序 獲取當(dāng)前的地理位置和速度

2020-09-05 14:19 更新

swan.getLocation

在工具和真機(jī)中的實(shí)現(xiàn)有區(qū)別,詳見(jiàn) API 實(shí)現(xiàn)差異。
解釋?zhuān)?獲取當(dāng)前的地理位置、速度。當(dāng)用戶(hù)離開(kāi)智能小程序后,此接口無(wú)法調(diào)用。使用該 API 需通過(guò)獲取用戶(hù)授權(quán)設(shè)置申請(qǐng)授權(quán)后方可對(duì)用戶(hù)發(fā)起授權(quán)申請(qǐng),使用 permission 對(duì)獲取位置信息的用途進(jìn)行說(shuō)明,可在  swan.authorize 中查看相關(guān)錯(cuò)誤碼信息。

方法參數(shù)

Object object

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

屬性名 類(lèi)型 必填 默認(rèn)值 說(shuō)明
type String wgs84 返回 gps 坐標(biāo),可選 gcj02 。 wgs84 返回 gps 坐標(biāo), gcj02 返回火星坐標(biāo), gcj02 比 wgs84 更為精確,所以返回可用于傳入 swan.openLocation 的坐標(biāo)
altitude Boolean 傳入 true 會(huì)返回高度信息,獲取高度需要較高精度且需要打開(kāi) gps ,會(huì)很耗時(shí),默認(rèn)沒(méi)有用 gps
success Function 接口調(diào)用成功的回調(diào)函數(shù),返回內(nèi)容詳見(jiàn)返回參數(shù)說(shuō)明
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success 返回參數(shù)說(shuō)明

參數(shù) 參數(shù)類(lèi)型 說(shuō)明
latitude Number 緯度,浮點(diǎn)數(shù),范圍為 -90~90 ,負(fù)數(shù)表示南緯
longitude Number 經(jīng)度,浮點(diǎn)數(shù),范圍為 -180~180 ,負(fù)數(shù)表示西經(jīng)
speed Number 速度,浮點(diǎn)數(shù),單位 m/s
accuracy Number 位置的精確度
altitude Number 高度,單位 m
verticalAccuracy Number 垂直精度,單位 m(Android 無(wú)法獲取,返回 0)
horizontalAccuracy Number 水平精度,單位 m
street String 街道名稱(chēng)
cityCode String 城市編碼,更新的行政區(qū)域信息數(shù)據(jù)請(qǐng)參考行政區(qū)域數(shù)據(jù)更新映射關(guān)系表
city String 城市名稱(chēng)
country String 國(guó)家
countryCode String 國(guó)家代碼
province String 省份
streetNumber String 街道號(hào)碼
district String 區(qū)

示例


圖片示例

代碼示例 

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

<view class="wrap">
    <view class="card-area">
        <view class="list-area border-bottom" s-for="item in infoList">
            <view class="list-item-key-4">{{item.chineseName}}}</view>
            <view class="list-item-value">{{item.value}}</view>
        </view>
    </view>
    <view class="swan-security-padding-bottom flex-button">
        <button type="primary" bindtap="getLocation">點(diǎn)擊獲取位置信息</button>
    </view>
</view>
Page({
    data: {
        infoList: [{
            chineseName: '精確度',
            engName: 'accuracy',
            value: ''
        }, {
            chineseName: '高度',
            engName: 'altitude',
            value: ''
        }, {
            chineseName: '城市名稱(chēng)',
            engName: 'city',
            value: ''
        }, {
            chineseName: '城市編碼',
            engName: 'cityCode',
            value: ''
        }, {
            chineseName: '國(guó)家',
            engName: 'country',
            value: ''
        }, {
            chineseName: '國(guó)家代碼',
            engName: 'countryCode',
            value: ''
        }, {
            chineseName: '區(qū)',
            engName: 'district',
            value: ''
        }, {
            chineseName: '水平精度',
            engName: 'horizontalAccuracy',
            value: ''
        }, {
            chineseName: '緯度',
            engName: 'latitude',
            value: ''
        }, {
            chineseName: '經(jīng)度',
            engName: 'longitude',
            value: ''
        }, {
            chineseName: '省份',
            engName: 'province',
            value: ''
        }, {
            chineseName: '速度',
            engName: 'speed',
            value: ''
        }, {
            chineseName: '街道名稱(chēng)',
            engName: 'street',
            value: ''
        }, {
            chineseName: '街道號(hào)碼',
            engName: 'streetNumber',
            value: ''
        }, {
            chineseName: '垂直精度',
            engName: 'verticalAccuracy',
            value: ''
        }]
    },
    onShow() {
        let infoList = this.getData('infoList');
        for (var i = 0; i < infoList.length; i++) {
            infoList[i].value = '';
        }
        this.setData({
            infoList
        });
    },
    getLocation(e) {
        swan.getLocation({
            type: 'gcj02',
            altitude: true,
            success: res => {
                console.log('getLocation success', res);
                let infoList = this.getData('infoList');
                for (var i = 0; i < infoList.length; i++) {
                    const engName = infoList[i].engName;
                    console.log('engName', res[engName]);
                    if (res[engName] === '' || res[engName] === 0 || res[engName] === '0') {
                        infoList[i].value = '暫無(wú)';
                    }
                    else {
                        engName === 'latitude' || engName === 'longitude'
                            ? infoList[i].value = this.formatLocation(res[engName]) + '′'
                            : infoList[i].value = res[engName];
                    }
                }
                this.setData({
                    infoList
                });
            },
            fail: err => {
                swan.showToast({
                    title: '獲取失敗,請(qǐng)檢查位置授權(quán)是否開(kāi)啟',
                    icon: 'none'
                });
            }
        });
    },
    formatLocation(data) {
        return data.toFixed(2).replace('.', '°');
    }
});

參考示例

圖片示例

同一位置下 type 屬性為 wgs84 : 

同一位置下 type 屬性為 gcj02 : 

參考示例:圖示可知 type 屬性為 gcj02 的位置更為精準(zhǔn),建議與 swan.openLocation 連用驗(yàn)證 

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

Page({
    getLocation() {
        swan.getLocation({
            type: 'gcj02', 
            altitude: true,
            success: res => {
                console.log('success', res);
                swan.openLocation({
                    latitude: res.latitude,
                    longitude: res.longitude,
                    success: res => {
                        console.log('openLocation success', res);
                    },
                    fail: err => {
                        console.log('openLocation fail', err);
                    }
                });
            },
            fail: err => {
                swan.showToast({title: '獲取失敗'});
            }
        });
    },
});


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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)