123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- // pages/user/localtion/index.js
- const {
- globalData: {
- util: {
- regeneratorRuntime,
- api: {
- allDept: url
- },
- request: {
- get
- },
- navigateBack,
- navigate_auth_native,
- telephone_native
- }
- }
- } = getApp();
- Page({
-
- /**
- * 生命周期函数--监听页面加载
- */
- async onLoad() {
- this.p = get(url)
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- wx.getLocation({
- type: 'wgs84',
- success: async({
- latitude,
- longitude
- }) => {
- let items= await this.p //去分页后 去除花括号
- items.forEach(item => {
- let [x, y] = item.position.split(',')
- item.distance = this.getDistance(latitude, longitude, y, x) / 1000
- item.longlat = [x, y]
- if(item.deptName.indexOf("李楼")!=-1){
- item.img = '/images/address/lilouzheng.jpg'
- } else if (item.deptName.indexOf("白马寺") != -1) {
- item.img = '/images/address/baimasi.jpg'
- } else if (item.deptName.indexOf("关林") != -1) {
- item.img = '/images/address/guanlin.jpg'
- } else if (item.deptName.indexOf("古城") != -1) {
- item.img = '/images/address/gucheng.jpg'
- } else if (item.deptName.indexOf("科技园") != -1) {
- item.img = '/images/address/kejiyuan.jpg'
- } else if (item.deptName.indexOf("泉舜") != -1) {
- item.img = '/images/address/quanshun.jpg'
- } else if (item.deptName.indexOf("龙门街道") != -1) {
- item.img = '/images/address/longmenjiedao.jpg'
- } else if (item.deptName.indexOf("太康") != -1) {
- item.img = '/images/address/taikangdonglu.jpg'
- } else if (item.deptName.indexOf("学府") != -1) {
- item.img = '/images/address/xuefujie.jpg'
- } else if (item.deptName.indexOf("安乐") != -1) {
- item.img = '/images/address/anlezhenzhengfu.jpg'
- } else if (item.deptName.indexOf("开元") != -1) {
- item.img = '/images/address/kaiyuanlu.jpg'
- }else{
- item.img = '/images/address/luoyang.jpg'
- }
- })
- this.setData({
- items: items.sort((a, b) => a.distance - b.distance).map(({ deptName: name, img,deptLeader: tel, distance,longlat})=>({
- name, tel, distance, img,longlat
- })),
- })
- },
- fail(e) {
- wx.getSetting({
- success({
- authSetting
- }) {
- if (!authSetting['scope.userLocation']) {
- wx.showModal({
- title: '是否授权当前位置',
- content: '需要获取您的地理位置,请确认授权,否则地图功能将无法使用',
- success: function(tip) {
- if (tip.confirm) {
- wx.openSetting({
- success: function(data) {
- if (data.authSetting["scope.userLocation"] === true) {
- wx.showToast({
- title: '授权成功',
- icon: 'success',
- duration: 1000
- })
- }
- }
- })
- } else {
- // wx.showToast({
- // title: '授权失败',
- // icon: 'none',
- // duration: 1000
- // })
- navigateBack();
- }
- }
- })
- }
- }
- })
- }
- })
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function() {
- },
- getDistance: function(lat1, lng1, lat2, lng2) {
- lat1 = lat1 || 0;
- lng1 = lng1 || 0;
- lat2 = lat2 || 0;
- lng2 = lng2 || 0;
- var rad1 = lat1 * Math.PI / 180.0;
- var rad2 = lat2 * Math.PI / 180.0;
- var a = rad1 - rad2;
- var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
- var r = 6378137;
- return (r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))).toFixed(0)
- },
- map:function(e){
- wx.openLocation({
- latitude: parseFloat(e.currentTarget.dataset.w),
- longitude: parseFloat(e.currentTarget.dataset.j),
- name: e.currentTarget.dataset.name ? e.currentTarget.dataset.name:'',
- scale: 18
- })
- },
- navigate_auth_native,
- telephone_native
- })
|