uniapp打開地圖直接獲取位置的實(shí)現(xiàn)代碼
uniapp打開地圖直接獲取位置

uniapp官網(wǎng)文檔
<view class="map-content" @click.stop="kilometer(item)">
<view class="km">
{{item.distance||'0'}}km
</view>
</view>import map from '../../utils/map.js'
onLoad() {
let that = this
let addressInfo = getApp().globalData.addressInfo;
if (addressInfo) {
that.addressInfo = addressInfo
that.getOilList()
} else {
//這里是獲取地理位置
map.loadCity().then(res => {
that.addressInfo = getApp().globalData.addressInfo
that.getOilList()
});
}
},
// 點(diǎn)擊獲取地圖
kilometer(e) {
uni.openLocation({
longitude: Number(e.lng),
latitude: Number(e.lat),
name: e.name,
address: e.address
})
},map.js頁面對(duì)地理位置進(jìn)行封裝
import QQMapWX from '@/utils/qqmap-wx-jssdk.min.js'
var qqmapsdk = {};
// 獲取位置授權(quán)
async function loadCity() {
let that = this;
return new Promise(function (resolve, reject) {
uni.getSetting({
success: (res) => {
// res.authSetting['scope.userLocation'] == undefined 表示 初始化進(jìn)入該頁面
// res.authSetting['scope.userLocation'] == false 表示 非初始化進(jìn)入該頁面,且未授權(quán)
// res.authSetting['scope.userLocation'] == true 表示 地理位置授權(quán)
if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
uni.showModal({
title: '請(qǐng)求授權(quán)當(dāng)前位置',
content: '需要獲取您的地理位置,請(qǐng)確認(rèn)授權(quán)',
success: function (res) {
if (res.cancel) {
uni.showToast({
title: '拒絕授權(quán)',
icon: 'none',
duration: 1000
})
reject(false);
} else if (res.confirm) {
uni.openSetting({
success: function (dataAu) {
if (dataAu.authSetting["scope.userLocation"] == true) {
uni.showToast({
title: '授權(quán)成功',
icon: 'success',
duration: 1000
})
that.getLocation().then(function (res) {
if (res) {
resolve(true);
} else {
reject(false);
}
});
} else {
uni.showToast({
title: '授權(quán)失敗',
icon: 'none',
duration: 1000
})
reject(false);
}
}
})
}
}
})
} else if (res.authSetting['scope.userLocation'] == undefined) {
that.getLocation().then(function (res) {
if (res) {
resolve(true);
} else {
reject(false);
}
});
} else {
that.getLocation().then(function (res) {
if (res) {
resolve(true);
} else {
reject(false);
}
});
}
}
})
}).catch((e) => {})
}
//坐標(biāo)獲取城市
function getLocation() {
let vm = this;
return new Promise(function (resolve, reject) {
uni.getLocation({
type: 'wgs84',
success: function (res) {
getApp().globalData.latitude = res.latitude;
getApp().globalData.longitude = res.longitude;
uni.setStorageSync("longitude", res.longitude)
uni.setStorageSync("latitude", res.latitude)
vm.getLocal().then(function (res) {
if (res) {
resolve(true);
} else {
reject(false);
}
});
},
fail: function (res) {
reject(false);
}
})
}).catch((e) => {})
}
// 坐標(biāo)轉(zhuǎn)換地址
function getLocal() {
let vm = this;
return new Promise(function (resolve, reject) {
qqmapsdk = new QQMapWX ({
key: 'asdfghjklqwertyuiop' //這里自己的key秘鑰進(jìn)行填充
});
qqmapsdk.reverseGeocoder({
location: {
latitude: getApp().globalData.latitude,
longitude: getApp().globalData.longitude
},
success: function (res) {
getApp().globalData.addressInfo = res.result.address_component;
resolve(true);
},
fail: function (res) {
reject(false);
}
});
}).catch((e) => {})
}
function calculateDistance(latitude, longitude) {
let vm = this;
return new Promise(function (resolve, reject) {
qqmapsdk = new QQMapWX ({
key: 'asdfghjklqwertyuiop' //這里自己的key秘鑰進(jìn)行填充
});
qqmapsdk.calculateDistance({
to: [{
latitude: latitude, //商家的緯度
longitude: longitude, //商家的經(jīng)度
}],
success: function (res) {
resolve(res);
},
fail: function (res) {
reject(res);
}
});
}).catch((e) => {})
}
function selectLocation() {
let that = this;
return new Promise(function (resolve, reject) {
uni.getSetting({
success(res) {
// 只返回用戶請(qǐng)求過的授權(quán)
let auth = res.authSetting;
if (auth['scope.userLocation']) {
// 已授權(quán),申請(qǐng)定位地址
resolve(true)
} else if (auth['scope.userLocation'] === undefined) {
// 用戶沒有請(qǐng)求過的授權(quán),不需要我們主動(dòng)彈窗,微信會(huì)提供彈窗
resolve(true)
} else if (!auth['scope.userLocation']) {
// 沒有授權(quán)過,需要用戶重新授權(quán)
// 這個(gè)彈窗是為了實(shí)現(xiàn)點(diǎn)擊,不然openSetting會(huì)失敗
uni.showModal({
title: '是否授權(quán)當(dāng)前位置?',
content: '需要獲取您的地理位置,請(qǐng)確認(rèn)授權(quán),否則定位功能將無法使用',
success: res => {
if (res.confirm) {
uni.openSetting({
success(res) {
let setting = res.authSetting;
if (!setting['scope.userLocation']) {
uni.showToast({
title: '地址授權(quán)失敗,定位功能無法使用',
icon: 'none',
});
reject(false)
} else {
// 地址授權(quán)成功,申請(qǐng)定位地址
resolve(true)
}
},
fail(err) {
// 需要點(diǎn)擊,有時(shí)候沒有點(diǎn)擊,是無法觸發(fā)openSetting
console.log('open-setting-fail', err);
reject(false)
}
});
}
}
});
}
}
});
}).catch((e) => {})
}
module.exports = {
loadCity,
getLocation,
getLocal,
getLocation,
selectLocation,
calculateDistance
}到此這篇關(guān)于uniapp打開地圖直接獲取位置的文章就介紹到這了,更多相關(guān)uniapp獲取地圖位置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js設(shè)置文本框中焦點(diǎn)位置在最后的示例代碼(簡(jiǎn)單實(shí)用)
本篇文章主要是對(duì)js設(shè)置文本框中焦點(diǎn)位置在最后的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-03-03
JS實(shí)現(xiàn)Date日期格式的本地化的方法小結(jié)
為了更好的更新多語言日期的顯示,所以希望實(shí)現(xiàn)日期的本地化格式顯示要求,常規(guī)的特殊字符型格式化無法滿足顯示要求,這里整理了幾種我思考實(shí)現(xiàn)的本地化實(shí)現(xiàn)功能2024-06-06
fireworks菜單生成器mm_menu.js在 IE 7.0 顯示問題的解決方法
由于公司官網(wǎng)采用的是dreamwaver / fireworks 內(nèi)建的彈出式菜單的JS,在IE7下發(fā)現(xiàn)菜單項(xiàng)文字顯示都變成一排,無法正確瀏覽.2009-10-10
package.json與package-lock.json的區(qū)別及詳細(xì)解釋
不知道大家平時(shí)在開發(fā)中有沒有注意到,你的項(xiàng)目中有兩個(gè)文件:package.json,package-lock.json,應(yīng)該很多人平時(shí)都不會(huì)去關(guān)注這兩個(gè)文件有啥關(guān)系吧,這篇文章主要給大家介紹了關(guān)于package.json與package-lock.json的區(qū)別及詳細(xì)解釋,需要的朋友可以參考下2022-08-08
JavaScript塊級(jí)作用域綁定以及狀態(tài)提升詳解
這篇文章主要給大家介紹了關(guān)于JavaScript塊級(jí)作用域綁定以及狀態(tài)提升的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03

