微信小程序 地图组件使用

 本地图程序利用map组件,以及使用它的markers,polyline属性分别设置地图两个位置的标志以及他们之间的连线,利用腾讯地图的API对两个地点进行查询,动态设置地图的经纬度,以及动态初始化markers,polyline属性。

程序运行截图如下:

程序代码如下:

  1. app.json
{
"pages": [
"pages/index/index",
"pages/logs/logs",
"pages/Map/index"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle": "black"
},
"tabBar": {
"list": [
{
"pagePath": "pages/index/index",
"text": "个人主页",
"iconPath": "utils/images/b3.png",
"selectedIconPath": "utils/images/b4.png"
},
{
"pagePath": "pages/Map/index",
"text": "地图",
"iconPath": "utils/images/a1.png",
"selectedIconPath": "utils/images/a2.png"
}
]
}
}

 2. map/index.js

// pages/Map/index.js
var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
key: 'GEYBZ-IM4WJ-TK5FL-K7BKW-TDDV2-P5FGL'

});
Page({

/**
* 页面的初始数据
*/
data: {
start_lat: 0,
start_lon: 0,
end_lat:0,
end_lon:0,
markers: [
{
iconPath: '../../utils/images/qz.jpg',
id: 0,
latitude: 0,
longitude: 0,
width: 50,
height: 50
},
{
iconPath: '../../utils/images/qz.jpg',
id: 1,
latitude: 0,
longitude: 0,
width: 50,
height: 50
}
],
address: '',
polyline: [
{
points: [{
longitude: 0,
latitude: 0

}, {
latitude: 0,
longitude: 0
}],
color: "#ff6600",
width: 2,
dottedLine: false,
arrowLine: true,
borderColor: "#000",
borderWidth: 5
}
]
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var that = this;
let start_lat=0;
let start_lon=0;
wx.getLocation({
success: function (res) {
qqmapsdk.search({
keyword: '江西省南昌市新建区丰和南大道696号',
complete: function (res) {
console.log(res.data[0].location.lat);
console.log(res.data[0].location.lng);
start_lat = res.data[0].location.lat;
start_lon = res.data[0].location.lng;
that.setData({
start_lat: res.data[0].location.lat,
start_lon: res.data[0].location.lng,
"markers[0].latitude": res.data[0].location.lat,
"markers[0].longitude": res.data[0].location.lng,
"polyline[0].points[0].latitude": res.data[0].location.lat,
"polyline[0].points[0].longitude": res.data[0].location.lng,
})
}
});
qqmapsdk.search({
keyword: '江西省南昌市新建区红谷滩新区学府大道999号',
complete: function (res) {
that.setData({
end_lat: res.data[0].location.lat,
end_lon: res.data[0].location.lng,
"markers[1].latitude": res.data[0].location.lat,
"markers[1].longitude": res.data[0].location.lng,
"polyline[0].points[1].latitude": res.data[0].location.lat,
"polyline[0].points[1].longitude": res.data[0].location.lng,
// polyline: [{
// points: [{
// longitude: res.data[0].location.lng,
// latitude: res.data[0].location.lat

// }, {
// latitude: that.data.start_lat,
// longitude: that.data.start_lon
// }],
// color: "#ff6600",
// width: 2,
// dottedLine: false,
// arrowLine: true,
// borderColor: "#000",
// borderWidth: 5
// }]
});
}
})

},
})

},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {

},

/**
* 生命周期函数--监听页面显示
*/
onShow: function () {

},

/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {

},

/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {

},

/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {

},

/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {

},

/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {

}
})

​​​​​​​   3. map/index.wxml

<map class='map' latitude='{{start_lat}}' longitude='{{start_lon}}' polyline="{{polyline}}" markers='{{markers}}'>
</map>

   4.  map/index.wxss

page{
width: 100%;
height: 100%;
}
.map{
height: 100%;
width: 100%;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值