微信小程序調(diào)用騰訊地圖API文檔JavaScript?SDK和WebService?API詳細(xì)解讀
搜索:騰訊位置服務(wù)

找到API文檔:

入門中第一步:申請(qǐng)開發(fā)者密鑰key

前往控制臺(tái):

創(chuàng)建應(yīng)用并獲取key:

設(shè)置key的時(shí)候,還需要小程序的APPID。所以要前往微信公眾平臺(tái)中獲取小程序的APPID:

限制要求:

添加配額:

看清哪一個(gè)key并且設(shè)置配額。如果有多個(gè)key,也可以根據(jù)特別的某些key進(jìn)行分配額度:

下載地圖的SDK:

并放入項(xiàng)目中:

添加服務(wù)器域名白名單:

登錄微信公眾平臺(tái):

設(shè)置白名單:

搜索地址:

實(shí)際開發(fā)者工具中截圖:

坐標(biāo)地圖:

wxml:

最終展示: 點(diǎn)擊搜索周邊KFC就出現(xiàn)紅色的預(yù)設(shè)值坐標(biāo)的地址。

具體代碼:
map.js
// 引入SDK核心類
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
Page({
data: {
markers: []
},
onLoad: function () {
// 實(shí)例化API核心類
this.qqmapsdk = new QQMapWX({
key: '************************ // 替換為你的QQ地圖SDK密鑰
});
},
// 事件觸發(fā),調(diào)用接口
nearby_search: function () {
var _this = this;
// 調(diào)用接口
this.qqmapsdk.search({
keyword: 'kfc', // 搜索關(guān)鍵詞
location: '39.980014,116.313972', // 設(shè)置周邊搜索中心點(diǎn)
success: function (res) { // 搜索成功后的回調(diào)
var mks = [];
for (var i = 0; i < res.data.length; i++) {
mks.push({ // 獲取返回結(jié)果,放到mks數(shù)組中
title: res.data[i].title,
id: parseInt(res.data[i].id), // 將 id 轉(zhuǎn)換為整數(shù)形式
latitude: res.data[i].location.lat,
longitude: res.data[i].location.lng,
iconPath: "/assets/icon/position.png", // 圖標(biāo)路徑
width: 20,
height: 20
});
}
_this.setData({ // 設(shè)置markers屬性,將搜索結(jié)果顯示在地圖中
markers: mks
});
},
fail: function (res) {
console.log('搜索失敗', res);
},
complete: function (res) {
console.log('搜索完成', res);
}
});
}
});
wxml:
<!--綁定點(diǎn)擊事件-->
<button bindtap="nearby_search">搜索周邊KFC</button>
<!--地圖容器-->
<map id="myMap"
markers="{{markers}}"
style="width:100%;height:300px;"
longitude="116.313972"
latitude="39.980014" scale='16'>
</map>注意:
1 調(diào)用API有次數(shù)和額度限制。
2 調(diào)用的接口要開通相應(yīng)的接口權(quán)限。
示例2: “關(guān)鍵詞輸入提示”

接口調(diào)用說(shuō)明:

前往開通配額:

代碼:
wxml:

實(shí)際wxml:

**.js 注意js代碼不要全部拷貝,而是將methods的部分放在Page()中:

實(shí)際**.js:

最后展示:

調(diào)用WebService API

舉例:定位服務(wù) --IP定位

wxml:
<view class="container">
<view class="map-container">
<map id="map" latitude="{{latitude}}" longitude="{{longitude}}" markers="{{markers}}" style="width: 100%; height: 400px;"></map>
</view>
<view class="info-container">
<view class="info-item">
<text class="info-label">國(guó)家:</text>
<text class="info-value">{{nation}}</text>
</view>
<view class="info-item">
<text class="info-label">省份:</text>
<text class="info-value">{{province}}</text>
</view>
<view class="info-item">
<text class="info-label">城市:</text>
<text class="info-value">{{city}}</text>
</view>
</view>
</view>
js:
Page({
data: {
latitude: 0, // 地圖中心點(diǎn)緯度
longitude: 0, // 地圖中心點(diǎn)經(jīng)度
markers: [], // 地圖標(biāo)記點(diǎn)
nation: '', // 國(guó)家
province: '', // 省份
city: '', // 城市
},
onLoad: function () {
// 發(fā)起獲取當(dāng)前IP定位信息的請(qǐng)求
this.getLocationByIP();
},
getLocationByIP: function () {
// 替換成你自己申請(qǐng)的騰訊地圖API密鑰
const key = '************************';
const apiUrl = `https://apis.map.qq.com/ws/location/v1/ip?key=${key}`;
wx.request({
url: apiUrl,
method: 'GET',
success: (res) => {
console.log('IP定位結(jié)果:', res.data);
if (res.data.status === 0) {
const { location, ad_info } = res.data.result;
const { lat, lng } = location;
const { nation, province, city } = ad_info;
// 更新頁(yè)面數(shù)據(jù),顯示定位信息
this.setData({
latitude: lat,
longitude: lng,
markers: [{
id: 1,
latitude: lat,
longitude: lng,
title: city,
iconPath: '/images/location.png', // 可自定義標(biāo)記圖標(biāo)
width: 30,
height: 30
}],
nation: nation,
province: province,
city: city
});
} else {
wx.showToast({
title: '定位失敗,請(qǐng)稍后重試',
icon: 'none',
duration: 2000
});
}
},
fail: (error) => {
console.error('請(qǐng)求失?。?, error);
wx.showToast({
title: '網(wǎng)絡(luò)請(qǐng)求失敗,請(qǐng)檢查網(wǎng)絡(luò)后重試',
icon: 'none',
duration: 2000
});
}
});
}
});
wxss:
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 20px;
}
.map-container {
width: 100%;
margin-bottom: 20px;
}
.info-container {
width: 100%;
background-color: #f0f0f0;
padding: 10px;
border-radius: 8px;
}
.info-item {
display: flex;
flex-direction: row;
margin-bottom: 5px;
}
.info-label {
font-weight: bold;
}
.info-value {
margin-left: 5px;
}
最終展示:

總結(jié)
到此這篇關(guān)于微信小程序調(diào)用騰訊地圖API文檔JavaScript SDK和WebService API的文章就介紹到這了,更多相關(guān)微信小程序調(diào)用騰訊地圖API文檔內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS如何讓你的移動(dòng)端交互體驗(yàn)更加優(yōu)秀
現(xiàn)在在手機(jī)等移動(dòng)端設(shè)備訪問(wèn)的人越來(lái)越多,我們前端開發(fā)者一直致力于將設(shè)計(jì)稿還原成頁(yè)面,供用戶訪問(wèn)。但除高度還原設(shè)計(jì)稿外,交互上的良好體驗(yàn)也是我們應(yīng)該做到的。2021-05-05
JavaScript如何獲取到導(dǎo)航條中HTTP信息
這篇文章主要為大家詳細(xì)介紹了JavaScript如何獲取到導(dǎo)航條中HTTP信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

