最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2024年08月03日 11:40:47   作者:愿?  
這篇文章主要介紹了uniapp打開地圖直接獲取位置的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

uniapp打開地圖直接獲取位置

uniapp官網(wǎng)文檔

https://en.uniapp.dcloud.io/api/location/open-location.html

<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()
				});
			}
		},
// 點擊獲取地圖
	kilometer(e) {
		uni.openLocation({
			longitude: Number(e.lng),
			latitude: Number(e.lat),
			name: e.name,
			address: e.address
		})
	},

map.js頁面對地理位置進行封裝

import QQMapWX from '@/utils/qqmap-wx-jssdk.min.js'
var qqmapsdk = {};
// 獲取位置授權
async function loadCity() {
    let that = this;
    return new Promise(function (resolve, reject) {
        uni.getSetting({
            success: (res) => {
                // res.authSetting['scope.userLocation'] == undefined    表示 初始化進入該頁面
                // res.authSetting['scope.userLocation'] == false    表示 非初始化進入該頁面,且未授權
                // res.authSetting['scope.userLocation'] == true    表示 地理位置授權
                if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
                    uni.showModal({
                        title: '請求授權當前位置',
                        content: '需要獲取您的地理位置,請確認授權',
                        success: function (res) {
                            if (res.cancel) {
                                uni.showToast({
                                    title: '拒絕授權',
                                    icon: 'none',
                                    duration: 1000
                                })
                                reject(false);
                            } else if (res.confirm) {
                                uni.openSetting({
                                    success: function (dataAu) {
                                        if (dataAu.authSetting["scope.userLocation"] == true) {
                                            uni.showToast({
                                                title: '授權成功',
                                                icon: 'success',
                                                duration: 1000
                                            })
                                            that.getLocation().then(function (res) {
                                                if (res) {
                                                    resolve(true);
                                                } else {
                                                    reject(false);
                                                }
                                            });
                                        } else {
                                            uni.showToast({
                                                title: '授權失敗',
                                                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) => {})
}
//坐標獲取城市
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) => {})
}
// 坐標轉換地址
function getLocal() {
    let vm = this;
    return new Promise(function (resolve, reject) {
        qqmapsdk = new QQMapWX ({
            key: 'asdfghjklqwertyuiop' //這里自己的key秘鑰進行填充
        });
        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秘鑰進行填充
        });
        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) {
                // 只返回用戶請求過的授權
                let auth = res.authSetting;
                if (auth['scope.userLocation']) {
                    // 已授權,申請定位地址
                    resolve(true)
                } else if (auth['scope.userLocation'] === undefined) {
                    // 用戶沒有請求過的授權,不需要我們主動彈窗,微信會提供彈窗
                    resolve(true)
                } else if (!auth['scope.userLocation']) {
                    // 沒有授權過,需要用戶重新授權
                    // 這個彈窗是為了實現(xiàn)點擊,不然openSetting會失敗
                    uni.showModal({
                        title: '是否授權當前位置?',
                        content: '需要獲取您的地理位置,請確認授權,否則定位功能將無法使用',
                        success: res => {
                            if (res.confirm) {
                                uni.openSetting({
                                    success(res) {
                                        let setting = res.authSetting;
                                        if (!setting['scope.userLocation']) {
                                            uni.showToast({
                                                title: '地址授權失敗,定位功能無法使用',
                                                icon: 'none',
                                            });
                                            reject(false)
                                        } else {
                                            // 地址授權成功,申請定位地址
                                            resolve(true)
                                        }
                                    },
                                    fail(err) {
                                        // 需要點擊,有時候沒有點擊,是無法觸發(fā)openSetting
                                        console.log('open-setting-fail', err);
                                        reject(false)
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    }).catch((e) => {})
}
module.exports = {
    loadCity,
    getLocation,
    getLocal,
    getLocation,
    selectLocation,
    calculateDistance
}

到此這篇關于uniapp打開地圖直接獲取位置的文章就介紹到這了,更多相關uniapp獲取地圖位置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

洪雅县| 观塘区| 甘谷县| 皋兰县| 伊金霍洛旗| 迁安市| 洪江市| 无为县| 土默特右旗| 成武县| 宁化县| 历史| 开封市| 栾城县| 铜梁县| 原阳县| 浦东新区| 准格尔旗| 五大连池市| 萍乡市| 左云县| 应用必备| 内江市| 法库县| 蒲城县| 乌拉特中旗| 嘉义县| 五河县| 大余县| 包头市| 湖口县| 鄂伦春自治旗| 汝南县| 桑植县| 清远市| 莆田市| 无锡市| 邵阳县| 大石桥市| 苏尼特左旗| 万载县|