小程序开发之Map地图组件详解
创作时间:
作者:
@小白创作中心
小程序开发之Map地图组件详解
引用
1
来源
1.
https://www.cnblogs.com/limaostudio/articles/13617986.html
Map组件简介
Map组件是小程序中的原生组件,使用时需要注意相关限制。个性化地图能力可以在小程序后台“设置-开发者工具-腾讯位置服务”申请开通。设置subkey后,小程序内的地图组件均会使用该底图效果,底图场景的切换会在后续版本提供。
本文主要展示地图组件的几个核心功能:
- 经纬度转预览图
- 经纬度转大图
- 地理位置转经纬度
- 预览图缩放
最终效果如下:
Map组件代码实现
HTML部分
<view class="page-body">
<view class="page-section page-section-gap">
<map
id="myMap"
style="width: 100%; height: 300px;"
latitude="{{latitude}}"
longitude="{{longitude}}"
markers="{{markers}}"
covers="{{covers}}"
show-location
></map>
</view>
<view class="btn-area">
<button bindtap="chooseLocation" class="page-body-button" type="primary">选择位置</button>
<button bindtap="openLocation" class="page-body-button" type="primary">打开位置</button>
<button bindtap="getCenterLocation" class="page-body-button" type="primary">获取位置</button>
<button bindtap="moveToLocation" class="page-body-button" type="primary">移动位置</button>
<button bindtap="translateMarker" class="page-body-button" type="primary">移动标注</button>
<button bindtap="includePoints" class="page-body-button" type="primary">缩放视野展示所有经纬度</button>
</view>
</view>
JavaScript部分
Page({
data: {
latitude: 23.099994,
longitude: 113.324520,
markers: [{
id: 1,
latitude: 23.099994,
longitude: 113.324520,
name: 'T.I.T 创意园'
}],
covers: [{
latitude: 23.099994,
longitude: 113.344520,
iconPath: '/image/location.png'
}, {
latitude: 23.099994,
longitude: 113.304520,
iconPath: '/image/location.png'
}]
},
onReady: function (e) {
this.mapCtx = wx.createMapContext('myMap')
},
chooseLocation:function(){
var that = this;
wx.chooseLocation({
success: function (res) {
console.log(res, "location")
console.log(res.name)
console.log(res.latitude)
console.log(res.longitude)
that.setData({
latitude: res.latitude,
longitude: res.longitude
})
},
fail: function () {
// fail
},
complete: function () {
// complete
}
})
},
openLocation:function(){
var that = this;
wx.openLocation({
latitude: that.data.latitude,
longitude: that.data.longitude,
scale: 18
})
},
getCenterLocation: function () {
this.mapCtx.getCenterLocation({
success: function(res){
console.log(res.longitude)
console.log(res.latitude)
}
})
},
moveToLocation: function () {
this.mapCtx.moveToLocation()
},
translateMarker: function() {
this.mapCtx.translateMarker({
markerId: 1,
autoRotate: true,
duration: 1000,
destination: {
latitude:23.10229,
longitude:113.3345211,
},
animationEnd() {
console.log('animation end')
}
})
},
includePoints: function() {
this.mapCtx.includePoints({
padding: [10],
points: [{
latitude:23.10229,
longitude:113.3345211,
}, {
latitude:23.00229,
longitude:113.3345211,
}]
})
}
})
CSS部分
.page-section-gap{
box-sizing: border-box;
padding: 0 30rpx;
}
.page-body-button {
margin-bottom: 30rpx;
}
总结
本文详细介绍了小程序开发中Map组件的使用方法,包括其核心功能和代码实现。通过本文,开发者可以快速掌握Map组件的使用技巧,为小程序开发提供便利。
本文原文来自Cnblogs
热门推荐
慈禧太后:用权谋驾驭宫廷斗争的47年统治
万年稻作文明启航地,浙江考古遗址公园群现新进展
宫廷燕窝:皇家养生珍品的现代食用指南
郴州东江湖:湖南最大人工湖,雾漫景观与六项国字号荣誉加持
《猎罪图鉴2》福州取景地全攻略:跟着剧集游榕城
熊胆粉的养生功效与使用禁忌:从传统到创新
冬季石林景区游玩攻略,门票优惠不容错过!
安康:千年古城展新颜
96.5%有效率!无花果叶熏洗治疗痔疮获临床证实
从构图到后期:专业摄影师的梅花拍摄完全指南
D-二聚体:敏感性高的血栓排除指标,还能预测疾病预后
实验联想法+口诀法,轻松搞定化学方程式
化学方程式书写技巧大揭秘!
新研究发现:吃了这类食物,肝脏都变好了!
广东惠州治愈系旅行,烟火惠州的6大绝美古村落
人都电麻了!到底谁能管一管静电啊?亲测最实用的一招是
洗衣机洗羽绒服会“爆炸”?换季了,羽绒服应该这么洗
在信息爆炸时代,如何理性上网并保护个人安全与隐私?
女生发抠鼻表情是什么意思?8招幽默回应化解尴尬!
剧看世界|她用当代喜剧解构世界政治
2024年压轴喜综开播,《喜剧大会》用幽默让平凡生活笑出花
南京必吃美食大盘点:鸭血粉丝、盐水鸭领衔27种经典
湛江发布旅游美食手绘地图,国庆畅游军港海岛特呈岛
兰州牛肉面汤料配方大揭秘:20种香料比例全公开!
南京博物院展出百件明清才女作品,展现古代女性艺术成就
不同年龄段的最佳睡眠时长揭秘
复旦研究:最佳睡眠时长竟是7小时!
秋游棋盘山:红枫秀湖与关东影视城,交通住宿全攻略
南宁地铁6号线最新进展:完成投资27亿元,创新融资模式破解建设难题
《水浒传》里的铁面孔目:裴宣的悲喜人生