小程序地图插入图标后 怎么实现点击图标弹出窗口
1问题描述
本次博客的要解决的问题是:小程序地图插入图标后 怎么实现点击图标弹出窗口
如图:
2算法描述点击标记点获取数据
想要在地图等页面上显示弹窗,是使用的是marke的方法,点击标记点获取数据
先在wxml里写好布局文件
<view class="detail-card-body1" hoverClass="hover"
bindtap="toCase1" data-id="{{ingma.id}}">
<view class="detail-card-title1" >{{ingma.name}}view>
<view class="detail-card-distance" >
距你{{dis}}米
view>
<view class="detail-card-distance ">
<view class="detail-card-audio-second ">
点击查看详情
view>
view>
<view class="detail-card-des1">{{detailCardInfo.cardDesc}}view>
view>
点击事件用bindtap,toCase1是需要在.js文件里写出的点击事件方法;bindtap="toCase1"是布局文件中删除按钮与.js文件里的删除方法绑定。下面看下.js里面跳转方法,弹窗是在点击图标时候弹出的,所以写在点击事件里面
Page({
data:{
latitude: 32.739668,//默认定位纬度
longitude: 103.834568,
showDialog:false,
mapId:"myMap",//wxml中的map的Id值
flag:true,
currentItemId:0,
isSelectedBuildType: 0,
islocation: true,
ingma:[],
poiT:[
{
name:'游览点',
fid:'1',
scale:'15',
latitude: "32.739668",
longitude: "103.834568",
markers:[]
} ,{
name:'科普点',
fid:'2',
scale:'14.5',
latitude: "32.722861",
longitude: "103.832321",
markers:[]
},{
name:'售票处',
fid:'3',
scale:'15.9',
latitude: "32.748993",
longitude: "103.822896",
markers:[]
},{
name: '卫生间',
fid:'4',
scale:'16',
latitude: "32.742095",
longitude: "103.834076",
markers:[]
}
],
onReady: function (e) {
// 使用 wx.createMapContext 获取 map 上下文
this.mapCtx = wx.createMapContext('myMap')
},
onLoad: function() {
var that = this;
// 获取当前定位的经纬度信息
wx.showLoading({
title:"定位中",
mask:true
})
wx.getLocation({
type: 'gcj02', //返回可以用于wx.openLocation的经纬度
altitude:true,//高精度定位
//定位成功,更新定位结果
success: function (res) {
var latitude = res.latitude;
var longitude = res.longitude;
that.setData({
centerX: longitude,
centerY: latitude,
});
},
//定位失败回调
fail:function(){
wx.showToast({
title:"定位失败",
icon:"none"
})
},
complete:function(){
//隐藏定位中信息进度
wx.hideLoading()
}
})
},
//点击其他位置关闭弹窗
close(){
this.setData({
flag:true,
locat:false,
})
},
//加载
onLoad:function(e){
//添加markers
var that = this
wx.request({
url: 'http://36.133.200.169:8088/public/spg/poi/list',
data: {
},
header: {'content-type':'application/json'},
method: 'POST',
dataType:'json',
responseType: 'text',
success: (result) => {
for (var i = 0; i < result.data.data.length; i++) {
var poiType = result.data.data[i].poiType
if ( poiType == '游览点'){
let mark = "poiT[0].markers[" + i + "]";
that.setData({
[mark]:
{
id: i,
iconPath: "/image/zoo.png",
latitude: result.data.data[i].latitude,
longitude: result.data.data[i].longitude,
width: 50,
height: 50,
name:result.data.data[i].name,
poiType:result.data.data[i].poiType,
label:{
content:result.data.data[i].name,
fontsize:15,
borderRadius:10,
bgColor:'#fff',
anchorX:-35,
anchorY:3,
padding:5
}
},
}
)} else if ( poiType == '科普点'){
let mark = "poiT[1].markers[" + i + "]";
that.setData({
[mark]:
{
id: i,
iconPath: "/image/kepu.png",
latitude: result.data.data[i].latitude,
longitude: result.data.data[i].longitude,
width: 50,
height: 50,
name:result.data.data[i].name,
poiType:result.data.data[i].poiType,
label:{
content:result.data.data[i].name,
fontsize:15,
borderRadius:10,
bgColor:'#fff',
anchorX:-35,
anchorY:3,
padding:5
}
},
}
)} else if ( poiType == '售票处'){
let mark = "poiT[2].markers[" + i + "]";
that.setData({
[mark]:
{
id: i,
iconPath: "/image/yld.png",
latitude: result.data.data[i].latitude,
longitude: result.data.data[i].longitude,
width: 50,
height: 50,
name:result.data.data[i].name,
poiType:result.data.data[i].poiType,
label:{
content:result.data.data[i].name,
fontsize:15,
borderRadius:10,
bgColor:'#fff',
anchorX:-35,
anchorY:3,
padding:5
}
},
}
)} else{
let mark = "poiT[3].markers[" + i + "]";
that.setData({
[mark]:
{
id: i,
iconPath: "/image/cs.png",
latitude: result.data.data[i].latitude,
longitude: result.data.data[i].longitude,
width: 50,
height: 50,
name:result.data.data[i].name,
poiType:result.data.data[i].poiType,
label:{
content:result.data.data[i].name,
fontsize:15,
borderRadius:10,
bgColor:'#fff',
anchorX:-35,
anchorY:3,
padding:5
}
},
}
)}
}
},
})
},
//点击标记点获取数据
markertap(res){
let self = this
console.log(res)
var mark=res.currentTarget.dataset;
console.log(mark)
var len = Object.keys(mark).length//获取JSON长度;
var markerId=res.markerId;
console.log(markerId)
var currentItemId= this.data.currentItemId
console.log(currentItemId)
var result = this.data.poiT[currentItemId].markers;
console.log(result)
var id=markerId
wx.getLocation({
type: 'gcj02',
success: (e) => {
var lat0 = e.latitude
var lng0 = e.longitude
var lat = result[markerId].latitude
var lgt =result[markerId].longitude
var dis =Math.floor(comm.GetDistance(lng0,lat0,lgt,lat));
console.log('距你'+dis+'米')
// console.log(result[markerId])
self.setData({
ingma:result[markerId],
dis:dis,
})
this.setData({
flag:false,
locat:true
})
},
})
},
toggleDialog: function () {
this.setData({
showDialog: false,
})
},
//跳转
toCase1(e){
console.log(e.currentTarget.dataset.id)
const id=e.currentTarget.dataset.id
wx.navigateTo({
url: '/packageA/page/case1/case1?id='+id,
})
},
})
3 结语
本次解决了小程序地图插入图标后 怎么实现点击图标弹出窗口的问题。这里使用到的是小程序自定义map组件标记点marker,这次只是介绍了marker的一个用法,如果后续有需要,会再出一期关于小程序地图的学习与使用。
相关文章
- c#启动外部程序_winform程序自动更新
- 微信小程序下订单支付代码实现
- c语言定时器实验程序,C语言定时器实验.doc[通俗易懂]
- 随机抽奖小程序_在线随机抽号小程序
- 小程序怎么和数字媒体结合才能实现价值最大化?
- 【说站】云开发【星座测评小程序+流量主】星座运势小程序源码
- 【愚公系列】2022年09月 微信小程序-WebGL立体图形的绘制
- 微信小程序+PHP实现登录注册(手把手教程)[通俗易懂]
- C语言中u8 u16 u32含义,有关stm32的问题,程序里面的u8、u16这些是什么意思啊「建议收藏」
- 强化学习 Q学习原理及例子(离散)附matlab程序
- 【愚公系列】2022年10月 微信小程序-优购电商项目-自定义组件
- 利用ICMPv4协议实现一个ping程序
- 1行Python代码,计算程序的运行时间,也可以用在算法和接口的调优上
- 微信小程序Video组件实现视频、自定义播放按钮、封面图、封面图文字demo
- java和vue开发的二手车小程序系统租车小程序系统
- 小程序wx.showToast真机不显示?
- EasyWechat 4.x 微信小程序企业付款到零钱
- io_uring 从原理到动手实践 part1: 使用系统调用接口实现 cat 程序
- 【Android 逆向】Android 逆向通用工具开发 ( adb forward 网络端口重定向命令 | PC 端逆向程序主函数分析 )
- SQLServer 错误 33081 由于 Authenticode 签名或文件路径无效,未能加载加密提供程序“%.*ls”。 请检查以前的消息,了解其他失败信息。 故障 处理 修复 支持远程
- 使用Linux中的For循环实现简单程序(linux的for循环)
- Linux自动启动程序的实现(linux程序自动启动)
- 程序 Linux应用调用驱动程序:开启新的可能(linux应用调用驱动)
- Linux系统编程指南:实现你的程序梦想(Linux系统怎么编程)
- 为Linux加入F77: 实现新程序的可能性(f77linux)
- 使用linux exec命令实现程序替换功能(linuxexec命令)
- 深入了解Linux程序的运行状态,掌握关键技巧(linux程序运行状态)
- Linux实现延时启动程序:提升启动效率的方法(linux延时启动程序)
- 部署Gitlab部署Redis加速程序性能(redis在gitlab)
- 如何在Linux上写串口程序(linux写串口)
- Redis解决程序异常能否实现回滚(程序异常redis回滚吗)
- 如何实现程序自动启动Redis数据库(怎么自动启动redis库)
- 用asp实现文件浏览、上传、下载的程序
- 用正则和xmlHttp实现的asp小偷程序
- asp中实现随机分组程序的代码
- php批量替换程序的具体实现代码