百度小程序 獲取城市 位置

1.app.js

App({
    globalData: {
        url_host:'https://appapi.5i5j.com',
        curCity:'',
    },
    onLaunch(options) {
        var that = this;
        // do something when launch
        // 添加到我的小程序引導,參見文檔: http://smartprogram.baidu.com/docs/design/component/guide_add/
        if (swan.canIUse('showFavoriteGuide')) {
            swan.showFavoriteGuide({
                type: 'bar',
                content: '一鍵添加到我的小程序',
                success(res) {
                    console.log('添加成功:', res);
                },
                fail(err) {
                    console.log('添加失敗:', err);
                }
            });
        }
    },
    //城市定位
    getLocationCity:function() {
        swan.getLocation({
            type: 'wgs84',
            altitude: true,
            success: res => {
                console.log('res:',res)
                this.globalData.curCity = res.city.substring(2,0)
                if (this.callback) { //這個函數名字和你定義的一樣即可
                    this.callback() //執行定義的回調函數
                }
            },
            fail: err => {
                console.log('getLocation fail', res)
            },
            complete: () => {
            
            }
        });
    },
    onShow() {

    },
    onHide() {
        // do something when hide
    }
});

2. index.js

var app = getApp();

var url_host = app.globalData.url_host;

Page({

data: {

loading: true,

cityId:'',

curCity:'',

},

onLoad: function () {

var that = this;

app.getLocationCity();//城市定位方法

that.getCityData();

},

//獲取城市

getCityData: function(){

var that = this;

if (app.globalData.curCity) {//判斷有沒有值 沒有說明還沒返回或者是失敗了

            console.log('第一次回調', app.globalData.curCity);

            that.setData({

curCity: app.globalData.curCity,

            })

console.log('curCity1:',that.data.curCity);

        } else { //請求成功後調用

            app.callback = () => {

                console.log('curCity回調:', app.globalData.curCity);

                that.setData({

curCity: app.globalData.curCity,

                })

console.log('curCity2:', that.data.curCity);

            };

        }

swan.request({

url: url_host + '/appapi/home/switchweb',

data: {},

method: "GET",

header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },

success: function (res) {

if (res.data.status == 200) {

var obj = res.data.data.find(function (obj) {

                        //return obj.name === "蘇州"

console.log('that.data.curCity:',that.data.curCity)

if(that.data.curCity === obj.name){

return that.data.curCity = obj.id

}

                    })

}

},

fail: function (res) {

console.log('error', res);//404

},

complete: function (res) {



}

})

},

//跳轉頁面

choosePage: function(e) {

var type = e.currentTarget.dataset.type;

switch (type) {

case 'switch': //選擇城市

swan.navigateTo({ url: '../switch/switch' });break;

case 'search': //搜索頁面

swan.navigateTo({ url: '../search/search' });break;

case 'sale': //二手房列表

swan.navigateTo({ url: '../sale/sale' });break;

case 'rent': //租房列表

swan.navigateTo({ url: '../rent/rent' });break;

case 'community': //小區列表

swan.navigateTo({ url: '../community/community' });break;

case 'broker': //經紀人列表

swan.navigateTo({ url: '../broker/broker' });break;

}

},

onReady: function() {

},

onShow: function() {

// 監聽頁面顯示的生命週期函數

},

onHide: function() {

// 監聽頁面隱藏的生命週期函數

},

onUnload: function() {

// 監聽頁面卸載的生命週期函數

},

onPullDownRefresh: function() {

// 監聽用戶下拉動作

},

onReachBottom: function() {

// 頁面上拉觸底事件的處理函數

},

onShareAppMessage: function () {

// 用戶點擊右上角轉發

}

});

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章