關於微信小程序map地圖的使用


結構部分: 

<map id='mymap' bindregionchange='bindregionchange' show-location longitude='{{longitude}}' latitude='{{latitude}}' markers='{{markers}}' class="maps" scale='18' >
<cover-image class='position_img' bindtap='getlocation' src='../images/position.png'></cover-image>
</map>
 
邏輯部分: 
    
// pages/index/index.js
const app = getApp()
Page({

/**
* 頁面的初始數據
*/
data: {
latitude: 22.3434,
longitude: 113.535,
markers: [
{
id: 1,
iconPath: '../images/core1.png',
latitude: 22.3434,
longitude: 113.535,
width: 40,
height: 40
}
],
 
},

/**
* 生命周期函數--監聽頁面加載
*/
onLoad: function (options) {
this.getLocationInfo()
console.log(app)
 
},

/**
* 生命周期函數--監聽頁面初次渲染完成
*/
onReady: function () {
var that = this
that.mapCtx = wx.createMapContext('mymap')
},

/**
* 生命周期函數--監聽頁面顯示
*/
onShow: function () {

},

/**
* 生命周期函數--監聽頁面隱藏
*/
onHide: function () {

},

/**
* 生命周期函數--監聽頁面卸載
*/
onUnload: function () {

},

/**
* 頁面相關事件處理函數--監聽用戶下拉動作
*/
onPullDownRefresh: function () {

},

/**
* 頁面上拉觸底事件的處理函數
*/
onReachBottom: function () {

},

/**
* 用戶點擊右上角分享
*/
onShareAppMessage: function () {

},
// 獲取用戶的位置
getLocationInfo() {
var that = this
wx.getLocation({
type: 'gcj02',
success: function(res) {
console.log(res)
that.setData({
longitude: res.longitude,
latitude: res.latitude,
markers: [
{
id: 1,
iconPath: '../images/core1.png',
latitude: res.latitude,
longitude: res.longitude,
width: 40,
height: 40
}
]
})
},
})
},
// d定位當前位置
getlocation() {
var that = this
wx.getLocation({
type: 'gcj02', //最好帶上type
success: function (res) {
console.log(res)
that.setData({
longitude: res.longitude,
latitude: res.latitude,
markers: [
{
id: 1,
iconPath: '../images/core1.png',
latitude: res.latitude,
longitude: res.longitude,
width: 40,
height: 40
}
]
})
},
})
},
// 當視圖發生變化時觸發
bindregionchange(e) {
var that = this
if(e.type=='end'){
that.mapCtx.getCenterLocation({
success: function(res) {
// console.log(res)
that.setData({
markers: [
{
id: 1,
iconPath: '../images/core1.png',
latitude: res.latitude,
longitude: res.longitude,
width: 40,
height: 40
}
]
})
}
})
}
}
})
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM