騰訊位置服務(wù)使用方法詳解(三)

騰訊位置服務(wù)使用方法詳解(一)
騰訊位置服務(wù)使用方法詳解(二)

4.個性化地圖樣式

為什么要用個性化地圖实蓬,提高不同場景下地圖的展現(xiàn)效果和用戶體驗允扇。


image

為什么選擇騰訊位置服務(wù)個性化地圖:

  • 全平臺通用
  • 開發(fā)成本極小
  • 個性化樣式支持動態(tài)更新
  • 支持全局配置和分級配置
  • 編輯平臺UI控件全面優(yōu)化
  • 每個元素可配置的屬性全部開放
  • 能夠支持自定義的地圖元素擴(kuò)充為52種

5.騰訊位置入門步驟

1.登錄騰訊位置服務(wù)


image

2.驗證手機(jī) 與 郵箱
3.申請開發(fā)密鑰(Key)
4.選擇您需要的產(chǎn)品

位置展示組件


image

路線規(guī)劃組件


image
image
image
地圖選點(diǎn)組件
image

前端定位組件


image

三.微信小程序JavaScript SDK

1.我申請了開發(fā)者密鑰key
2.開通webserviceAPI服務(wù):控制臺 -> key管理 -> 設(shè)置(使用該功能的key)-> 勾選webserviceAPI -> 保存
(小程序SDK需要用到webserviceAPI的部分服務(wù),所以使用該功能的KEY需要具備相應(yīng)的權(quán)限)
日調(diào)用量:1萬次 / Key----并發(fā)數(shù):5次 / key / 秒 蓬抄。

onLoad() {
    console.log('頁面加載了')
    // 實例化API核心類
    qqmapsdk = new QQMapWX({
        // key: '申請的key'
    });
},
onShow() {
    console.log('頁面顯示了')
    // 調(diào)用接口dadaqianduan
    qqmapsdk.search({
        keyword: '酒店',
        success: (res) => {
            console.log(res);
        },
        fail: (err) => {
            console.log(err);
        },
        complete: (cres) => {
            console.log(cres);
        }
    })
},

我返回的數(shù)據(jù)如圖:


image

QQMapWX – 小程序JavaScriptSDK核心類 – new QQMapWX(options:Object)


image
// 引入SDK核心類
var QQMapWX = require('xxx/qqmap-wx.js');
 
// 實例化API核心類
var demo = new QQMapWX({
    key: '開發(fā)密鑰(key)' // 必填
});

地點(diǎn)搜索:

// 地點(diǎn)搜索
nearbySearchBtn() {
    qqmapsdk.search({
        keyword: 'kfc', //搜索關(guān)鍵詞
        location: '39.980014,116.313972', //設(shè)置周邊搜索中心點(diǎn)
        success: (res) => {
            var mks = []
            for (var i = 0; i < res.data.length; i++) {
                mks.push({ // 獲取返回結(jié)果,放到mks數(shù)組中
                    title: res.data[i].title,
                    id: res.data[i].id,
                    latitude: res.data[i].location.lat,
                    longitude: res.data[i].location.lng,
                    iconPath: "/location.png", //圖標(biāo)路徑
                    width: 20,
                    height: 20
                })
            }
            this.markers = mks
        },
        fail: (res) => {
            console.log(res);
        },
        complete: (res) => {
            console.log(res);
        }
    });
},

效果如圖:


image
<script>
    // 引入SDK核心類碾局,js文件根據(jù)自己業(yè)務(wù)摆碉,位置可自行放置
    // var QQMapWX = require('../../js_sdk/wxmp-qqmap/qqmap-wx-jssdk.js');
    import QQMapWX from '../../js_sdk/wxmp-qqmap/qqmap-wx-jssdk.js';
    var qqmapsdk;
    export default {
        components: {},
        data() {
            return {
                backfill: '',
                markers: [],
                suggestion: [],
            }
        },
        onLoad() {
            console.log('頁面加載了') // 實例化API核心類
            qqmapsdk = new QQMapWX({ // key: '申請的key'
                key: '自己申請洒放,我的就不放出來了'
            });
        },
        onShow() {
            console.log('頁面顯示了') // 調(diào)用接口dadaqianduan
            qqmapsdk.search({
                keyword: '酒店',
                success: (res) => {
                    console.log(res);
                },
                fail: (err) => {
                    console.log(err);
                },
                complete: (cres) => {
                    console.log(cres);
                }
            })
        },
        onReady() {
            console.log('頁面初次渲染完成了')
        },
        methods: {
            getsuggest(e) {
                console.log(e.detail.value)
                qqmapsdk.getSuggestion({
                  keyword: e.detail.value, //用戶輸入的關(guān)鍵詞,可設(shè)置固定值,如keyword:'KFC'
                  //region:'北京', //設(shè)置城市名齐媒,限制關(guān)鍵詞所示的地域范圍蒲每,非必填參數(shù)
                  success: (res) => {//搜索成功后的回調(diào)
                    console.log(res);
                    var sug = [];
                    for (var i = 0; i < res.data.length; i++) {
                      sug.push({ // 獲取返回結(jié)果,放到sug數(shù)組中
                        title: res.data[i].title,
                        id: res.data[i].id,
                        addr: res.data[i].address,
                        city: res.data[i].city,
                        district: res.data[i].district,
                        latitude: res.data[i].location.lat,
                        longitude: res.data[i].location.lng
                      });
                    }
                     this.suggestion = sug
                  },
                  fail: (error)=> {
                    console.error(error);
                  },
                  complete: (res)=> {
                    console.log(res);
                  }
                });
            },
            backfillBtn(e) {
                var id = e.currentTarget.id;
                for (var i = 0; i < this.suggestion.length; i++) {
                    if (i == id) {
                        this.backfill = this.suggestion[i].title
                    }
                }
            },
            // 地點(diǎn)搜索
            nearbySearchBtn() {
                qqmapsdk.search({
                    keyword: 'kfc', //搜索關(guān)鍵詞
                    location: '39.980014,116.313972', //設(shè)置周邊搜索中心點(diǎn)
                    success: (res) => {
                        var mks = []
                        for (var i = 0; i < res.data.length; i++) {
                            mks.push({ // 獲取返回結(jié)果喻括,放到mks數(shù)組中
                                title: res.data[i].title,
                                id: res.data[i].id,
                                latitude: res.data[i].location.lat,
                                longitude: res.data[i].location.lng,
                                iconPath: "/static/location.png", //圖標(biāo)路徑
                                width: 20,
                                height: 20
                            })
                        }
                        this.markers = mks
                    },
                    fail: (res) => {
                        console.log(res);
                    },
                    complete: (res) => {
                        console.log(res);
                    }
                });
            },
        },
        onHide() {
            console.log('頁面隱藏了')
        },
    }
</script>

預(yù)覽效果如圖下:


image
<script>
    import QQMapWX from '../../js_sdk/wxmp-qqmap/qqmap-wx-jssdk.js';
    var qqmapsdk;
    export default {
        components: {},
        data() {
            return {
                backfill: '',
                markers: [],
                poi: {
                    latitude: 39.984060,
                    longitude: 16.307520
                },
            }
        },
        onLoad() {
            console.log('頁面加載了') // 實例化API核心類
            qqmapsdk = new QQMapWX({ // key: '申請的key'
                key: ''
            });
        },
        onShow() {
            console.log('頁面顯示了')
        },
        onReady() {
            console.log('頁面初次渲染完成了')
        },
        methods: {
            formSubmit(e) {
                qqmapsdk.reverseGeocoder({
                    location: e.detail.value.reverseGeo || '',
                    //獲取表單傳入的位置坐標(biāo),不填默認(rèn)當(dāng)前位置,示例為string格式
                    //get_poi: 1, //是否返回周邊POI列表:1.返回邀杏;0不返回(默認(rèn)),非必須參數(shù)
                    success: (res) => {
                        console.log(res);
                        var res = res.result;
                        var mks = [];
                        /**
                         *  當(dāng)get_poi為1時,檢索當(dāng)前位置或者location周邊poi數(shù)據(jù)并在地圖顯示唬血,可根據(jù)需求是否使用
                         *
                            for (var i = 0; i < result.pois.length; i++) {
                            mks.push({ // 獲取返回結(jié)果望蜡,放到mks數(shù)組中
                                title: result.pois[i].title,
                                id: result.pois[i].id,
                                latitude: result.pois[i].location.lat,
                                longitude: result.pois[i].location.lng,
                                iconPath: './resources/placeholder.png', //圖標(biāo)路徑
                                width: 20,
                                height: 20
                            })
                            }
                        *
                        **/
                        //當(dāng)get_poi為0時或者為不填默認(rèn)值時,檢索目標(biāo)位置拷恨,按需使用
                        mks.push({ // 獲取返回結(jié)果泣特,放到mks數(shù)組中
                            title: res.address,
                            id: 0,
                            latitude: res.location.lat,
                            longitude: res.location.lng,
                            iconPath: '/static/location.png', //圖標(biāo)路徑
                            width: 20,
                            height: 20,
                            callout: { //在markers上展示地址名稱,根據(jù)需求是否需要
                                content: res.address,
                                color: '#000',
                                display: 'ALWAYS'
                            }
                        });
                        this.markers = mks;
                        // this.poi = {
                        //  latitude: res.location.lat,
                        //  longitude: res.location.lng
                        // }
                    },
                    fail: (error) => {
                        console.error(error);
                    },
                    complete: (res) => {
                        console.log(res);
                    }
                })
            }
        },
        onHide() {
            console.log('頁面隱藏了')
        },
    }
</script>

geocoder – 提供由地址描述到所述位置坐標(biāo)的轉(zhuǎn)換挑随,與逆地址解析reverseGeocoder()的過程正好相反状您。
預(yù)覽效果如圖:


image
formSubmit(e) {
    //調(diào)用地址解析接口
    qqmapsdk.geocoder({
      //獲取表單傳入地址 e.detail.value.geocoder
      address: e.detail.value, //地址參數(shù)勒叠,例:固定地址,address: '北京市海淀區(qū)彩和坊路海淀西大街74號'
      success: (res)=> {//成功后的回調(diào)
        console.log(res);
        var res = res.result;
        var latitude = res.location.lat;
        var longitude = res.location.lng;
        //根據(jù)地址解析在地圖上標(biāo)記解析地址位置
         this.markers = [{
            id: 0,
            title: res.title,
            latitude: latitude,
            longitude: longitude,
            iconPath: '/static/location.png',//圖標(biāo)路徑
            width: 20,
            height: 20,
            callout: { //可根據(jù)需求是否展示經(jīng)緯度
              content: latitude + ',' + longitude,
              color: '#000',
              display: 'ALWAYS'
            }
          }],
          this.poi= { //根據(jù)自己data數(shù)據(jù)設(shè)置相應(yīng)的地圖中心坐標(biāo)變量名稱
            latitude: Number(latitude),
            longitude:  Number(longitude),
          }
      },
      fail: (error)=> {
        console.error(error);
      },
      complete: (res)=> {
        console.log(res);
      }
    })
}

預(yù)覽效果圖如下:


image
formSubmit(e){
        //調(diào)用距離計算接口
        console.log(this.start,'dadaqianduan')
        qqmapsdk.calculateDistance({
            //mode: 'driving',//可選值:'driving'(駕車)膏孟、'walking'(步行)眯分,不填默認(rèn):'walking',可不填
            //from參數(shù)不填默認(rèn)當(dāng)前地址
            //獲取表單提交的經(jīng)緯度并設(shè)置from和to參數(shù)(示例為string格式)
            // from: e.detail.value.start || '', //若起點(diǎn)有數(shù)據(jù)則采用起點(diǎn)坐標(biāo),若為空默認(rèn)當(dāng)前地址
            from: this.start || '',
            to: this.end,
            // to: e.detail.value.dest, //終點(diǎn)坐標(biāo)
            success: (res)=> {//成功后的回調(diào)
              console.log(res);
              var res = res.result;
              var dis = [];
              for (var i = 0; i < res.elements.length; i++) {
                dis.push(res.elements[i].distance); //將返回數(shù)據(jù)存入dis數(shù)組柒桑,
              }
              this.distance=dis
            },
            fail: (error)=> {
              console.error(error);
            },
            complete: (res)=> {
              console.log(res);
            }
        });
    }
},

調(diào)用獲取城市列表接口弊决,效果圖如下:


image
onShow() {
    console.log('頁面顯示了')
    //調(diào)用獲取城市列表接口
    qqmapsdk.getCityList({
        success: (res) => { //成功后的回調(diào)
            console.log(res);
            console.log('省份數(shù)據(jù):', res.result[0]); //打印省份數(shù)據(jù)
            this.a = res.result[0]
            console.log('城市數(shù)據(jù):', res.result[1]); //打印城市數(shù)據(jù)
            this.b = res.result[1]
            console.log('區(qū)縣數(shù)據(jù):', res.result[2]); //打印區(qū)縣數(shù)據(jù)
            this.c = res.result[2]
        },
        fail: (error) => {
            console.error(error);
        },
        complete: (res) => {
            console.log(res);
        }
    });
},

獲取城市區(qū)縣,效果圖如下:


image
onShow() {
    console.log('頁面顯示了')
    //調(diào)用獲取城市列表接口
    qqmapsdk.getCityList({
        success: (res) => { //成功后的回調(diào)
            console.log(res);
            console.log('省份數(shù)據(jù):', res.result[0])
            var city = res.result[0];
            //根據(jù)對應(yīng)接口getCityList返回數(shù)據(jù)的Id獲取區(qū)縣數(shù)據(jù)(以北京為例)
            qqmapsdk.getDistrictByCityId({
                // 傳入對應(yīng)省份ID獲得城市數(shù)據(jù)魁淳,傳入城市ID獲得區(qū)縣數(shù)據(jù),依次類推
                id: city[0].id, //對應(yīng)接口getCityList返回數(shù)據(jù)的Id飘诗,如:北京是'110000'
                success: (res) => { //成功后的回調(diào)
                    console.log(res);
                    console.log('對應(yīng)城市ID下的區(qū)縣數(shù)據(jù)(以北京為例):', res.result[0]);
                },
                fail: (error) => {
                    console.error(error);
                },
                complete: (res) => {
                    console.log(res);
                }
            });
        },
        fail: (error) => {
            console.error(error);
        },
        complete: (res) => {
            console.log(res);
        }
    });
},

騰訊位置服務(wù)為微信小程序提供了基礎(chǔ)的標(biāo)點(diǎn)能力、線和圓的繪制接口等地圖組件和位置展示界逛、地圖選點(diǎn)等地圖API位置服務(wù)能力支持昆稿,使得開發(fā)者可以自由地實現(xiàn)自己的微信小程序產(chǎn)品。 在此基礎(chǔ)上息拜,騰訊位置服務(wù)微信小程序JavaScript SDK是專為小程序開發(fā)者提供的LBS數(shù)據(jù)服務(wù)工具包溉潭,可以在小程序中調(diào)用騰訊位置服務(wù)的POI檢索、關(guān)鍵詞輸入提示少欺、地址解析喳瓣、逆地址解析、行政區(qū)劃和距離計算等數(shù)據(jù)服務(wù)赞别,讓您的小程序更強(qiáng)大畏陕!


image
image

image
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市仿滔,隨后出現(xiàn)的幾起案子蹭秋,更是在濱河造成了極大的恐慌,老刑警劉巖堤撵,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件仁讨,死亡現(xiàn)場離奇詭異,居然都是意外死亡实昨,警方通過查閱死者的電腦和手機(jī)洞豁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來荒给,“玉大人丈挟,你說我怎么就攤上這事≈镜纾” “怎么了曙咽?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長挑辆。 經(jīng)常有香客問我例朱,道長孝情,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任洒嗤,我火速辦了婚禮箫荡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘渔隶。我一直安慰自己羔挡,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布间唉。 她就那樣靜靜地躺著绞灼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪呈野。 梳的紋絲不亂的頭發(fā)上低矮,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天,我揣著相機(jī)與錄音际跪,去河邊找鬼商佛。 笑死喉钢,一個胖子當(dāng)著我的面吹牛姆打,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播肠虽,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了甲献?” 一聲冷哼從身側(cè)響起端壳,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎韩玩,沒想到半個月后垒玲,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡找颓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年合愈,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片击狮。...
    茶點(diǎn)故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡佛析,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出彪蓬,到底是詐尸還是另有隱情寸莫,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布档冬,位于F島的核電站膘茎,受9級特大地震影響桃纯,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜辽狈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一慈参、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧刮萌,春花似錦驮配、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至涮阔,卻和暖如春猜绣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背敬特。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工掰邢, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人伟阔。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓辣之,卻偏偏與公主長得像,于是被迫代替她去往敵國和親皱炉。 傳聞我的和親對象是個殘疾皇子怀估,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評論 2 354