123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- import { jsonp } from 'vue-jsonp';
- import store from '../store/index';
- export const navigationAddress = (destination) => {
- const locationData = destination.split(',');
- uni.openLocation({
- latitude: locationData[1] * 1,
- longitude: locationData[0] * 1,
- success: function () {
- console.log('success');
- },
- fail(err) {
- console.log(err);
- }
- });
-
-
-
-
- };
- export const getAdressDetailByLngLat = (lat, lng) => {
- return new Promise((resolve, reject) => {
-
- jsonp('https://restapi.amap.com/v3/geocode/regeo', {
- key: '5773f02930998e41b0de1d4e1bdbcaa9',
- location: `${lng},${lat}`
- })
- .then((res) => {
- console.log(res);
- resolve(res);
- })
- .catch((err) => {
- reject(err);
- });
-
-
- uni.request({
- url: 'https://restapi.amap.com/v3/geocode/regeo',
- data: {
- key: '5773f02930998e41b0de1d4e1bdbcaa9',
- location: `${lng},${lat}`
- },
- header: {},
- success: (res) => {
- resolve(res.data);
- },
- fail() {
- reject();
- }
- });
-
- });
- };
- export const getLngLatByAddress = (address) => {
- return new Promise((resolve, reject) => {
-
- jsonp('https://restapi.amap.com/v3/geocode/geo', {
- key: '5773f02930998e41b0de1d4e1bdbcaa9',
- address
- })
- .then((res) => {
- resolve(res);
- })
- .catch((err) => {
- reject(err);
- });
-
-
- uni.request({
- url: 'https://restapi.amap.com/v3/geocode/geo',
- data: {
- key: '5773f02930998e41b0de1d4e1bdbcaa9',
- address
- },
- header: {},
- success: (res) => {
- resolve(res.data);
- },
- fail() {
- reject();
- }
- });
-
- });
- };
- export function MapLoader(onSuccess, onFail) {
- let aMapScript = document.createElement('script');
- aMapScript.setAttribute('src', 'https://webapi.amap.com/maps?v=1.4.11&key=262e1be2edfaf66333664f33a915ccf3&plugin=AMap.CitySearch');
- document.head.appendChild(aMapScript);
- return (aMapScript.onload = function () {
- AMap.plugin('AMap.Geolocation', function () {
- var geolocation = new AMap.Geolocation({
- enableHighAccuracy: true,
- timeout: 10000,
- buttonOffset: new AMap.Pixel(10, 20),
- zoomToAccuracy: true,
- buttonPosition: 'RB'
- });
- geolocation.getCurrentPosition();
- AMap.event.addListener(geolocation, 'complete', onComplete);
- AMap.event.addListener(geolocation, 'error', onError);
- function onComplete(data) {
-
- const position = data.position;
- onSuccess &&
- typeof onSuccess === 'function' &&
- onSuccess({
- latitude: position.lat,
- longitude: position.lng
- });
- }
- function onError(data) {
- onFail && typeof onFail === 'function' && onFail(data);
- }
- });
- });
- }
- export const isUserEmpowerLocationPermission = () => {
- return new Promise(async (resolve, reject) => {
- if (!navigator.permissions && !navigator.permissions.query) {
- reject(false);
- }
- const permissionStatus = await navigator.permissions.query({
- name: 'geolocation'
- });
- const state = permissionStatus.state;
- if (state === 'granted') {
- resolve(true);
- } else if (state === 'denied') {
- reject(false);
- }
- setTimeout(() => {
- reject('prompt');
- }, 3000);
- });
- };
- export const getCurrentLocation = (error = true, isRedirectImmediately, isAwait) => {
- let count = 2;
- let timer = null;
- return new Promise(async (resolve, reject) => {
- let currentAddress = store.getters.detailAddress;
- if (currentAddress) {
- resolve(currentAddress);
- }
- if (isAwait) {
- resolve(currentAddress);
- }
- timer = setInterval(() => {
- if (count === 0) {
- if (!currentAddress) {
- error ? resolve('') : reject(new Error('获取您的定位失败'));
- }
- clearInterval(timer);
- if (isRedirectImmediately) {
- timer = setTimeout(() => {
- uni.redirectTo({
- url: '/pages/choose-location/choose-locatio'
- });
- }, 2000);
- }
- }
- count--;
- }, 1000);
- await store.dispatch('location/getCurrentLocation', (data) => {
- currentAddress = data.detail;
- if (currentAddress) {
- resolve(currentAddress);
- }
- });
- });
- };
|