123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- // #ifdef H5
- import { jsonp } from "vue-jsonp";
- // #endif
- // import wx from 'weixin-js-sdk'
- let locationLoading = false;
- export default function() {
- if (locationLoading) return;
- locationLoading = true;
- console.log("location start");
- // wx.config({
- // debug: false, // 开启调试模式,
- // appId: data.appId, //必填,企业号的唯一标识,此处企业号corpid
- // timestamp: data.timestamp, //必填,生成签名的时间戳
- // nonceStr: data.nonceStr, //必填,生成签名的随机串
- // signature: data.signature, //必填,签名,见附录1
- // jsApiList: ["chooseImage"], //必填,需要使用的JS接口列表JS接口列表见附录2
- // });
- return new Promise((resolve, reject) => {
- let locationType = "gcj02";
- // #ifdef APP-PLUS
- let ua = uni.getSystemInfoSync().platform;
- if (/ios/i.test(ua)) locationType = "wgs84";
- // #endif
- uni.getLocation({
- type: 'wgs84', //gcj02
- altitude: false,
- isHighAccuracy: false,
- geocode: true,
- success: function(res) {
- console.log("GPS", res);
- // const key = "MPRBZ-N4E66-BI3SY-M3L4N-S2AW5-BIBGN";
- let url = "";
- let latitude = res.latitude;
- let longitude = res.longitude;
- let data = {
- // ...res?.address,
- location: {
- lat: res.latitude,
- lng: res.longitude
- }
- };
- uni.setStorageSync('address', res);
- resolve(data);
- // let url = "https://apis.map.qq.com/ws/coord/v1/translate";
- // let data = {
- // locations: `${latitude},${longitude}`,
- // key,
- // output: "jsonp",
- // type: 1,
- // };
- // jsonp(url, data).then((res) => {
- // console.log("坐标转换", res);
- // if (res?.status == 0) {
- // const translateLocation = res?.locations;
- // if (translateLocation && translateLocation?.length) {
- // latitude = translateLocation[0]?.lat;
- // longitude = translateLocation[0]?.lng;
- // }
- // }
- // uni.request({
- // url: "https://api.niusenyun.com/api/v1/getRegionByLocation",
- // method: "POST",
- // data: {
- // latitude,
- // longitude,
- // },
- // success: (res) => {
- // console.log("geocoder", res);
- // if (res.data?.code == 0) {
- // const locationData = res.data?.data;
- // const address = locationData?.address_component ?? {};
- // const address_info = locationData?.ad_info ?? {};
- // const addressData = { ...address_info, ...address };
- // uni.setStorage({
- // key: "address",
- // data: addressData,
- // success: function () {},
- // });
- // resolve(addressData);
- // }
- // },
- // });
- // url = "https://apis.map.qq.com/ws/geocoder/v1/";
- // data = {
- // location: `${latitude},${longitude}`,
- // key,
- // output: "jsonp",
- // };
- // jsonp(url, data).then((res) => {
- // console.log("geocoder info", res);
- // const address = res.result?.address_component ?? {};
- // const address_info = res.result?.ad_info ?? {};
- // const location = res.result?.location ?? address_info?.location;
- // const addressData = {
- // ...address_info,
- // ...address,
- // location,
- // };
- // resolve(addressData);
- // });
- // });
- // #ifdef APP
- // const addressData = res?.address; addressData.location = {
- // lng: res?.longitude,
- // lat: res?.latitude,
- // };
- // uni.setStorage({
- // key: "address",
- // data: addressData,
- // success: function() {},
- // });
- // #endif
- },
- fail: function(ret) {
- console.log("定位失败", ret);
- reject(ret);
- },
- complete: function(ret) {
- locationLoading = false;
- },
- });
- });
- }
|