小編給大家分享一下微信小程序如何獲取位置展示地圖并標注信息,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
1.map組件的高度如果想要鋪滿屏幕,要是使用height:100vh樣式
2.獲取位置要在app.json中標明權限
3.先使用wx.getLocation獲取自己的位置,然后再回調中使用setData方法,賦予數據給前臺頁面展示標注點
index.js
//index.js
//獲取應用實例
const app = getApp()
Page({
data: {
},
onLoad: function () {
var self=this;
this.mapCtx = wx.createMapContext('myMap');
wx.getLocation({
type: 'gcj02',
success(res) {
self.setData({
latitude : res.latitude,
longitude : res.longitude,
markers: [{
id: 1,
latitude: res.latitude,
longitude: res.longitude,
iconPath: '/image/location.png',
callout:{
content:"服務:青少年英語培訓\r\n姓名:陶士涵\r\n電話:18808987876",
bgColor:"#fff",
padding:"5px",
borderRadius:"2px",
borderWidth:"1px",
borderColor:"#07c160",
}
},
{
id: 2,
latitude: res.latitude,
longitude: res.longitude+0.01,
iconPath: '/image/location.png',
callout: {
content: "服務:出租龍興園西區9號樓二單元501\r\n姓名:陶士涵\r\n電話:18808987876",
bgColor: "#fff",
padding: "5px",
borderRadius: "2px",
borderWidth: "1px",
borderColor: "#07c160",
}
}
],
});
}
})
},
})index.wxml
<!--index.wxml-->
<map
id="myMap"
latitude="{{latitude}}"
longitude="{{longitude}}"
markers="{{markers}}"
covers="{{covers}}"
show-location
></map>app.json
{
"pages": [
"pages/index/index",
"pages/logs/logs"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "找服務",
"navigationBarTextStyle": "black"
},
"sitemapLocation": "sitemap.json",
"permission": {
"scope.userLocation": {
"desc": "你的位置信息將用于獲取周邊服務"
}
}
}看完了這篇文章,相信你對“微信小程序如何獲取位置展示地圖并標注信息”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。