前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >小程序地图插入图标后 怎么实现点击图标弹出窗口

小程序地图插入图标后 怎么实现点击图标弹出窗口

作者头像
算法与编程之美
发布2023-01-03 19:42:07
1.5K0
发布2023-01-03 19:42:07
举报

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的一个用法,如果后续有需要,会再出一期关于小程序地图的学习与使用。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-12-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 算法与编程之美 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库一体机 TData
数据库一体机 TData 是融合了高性能计算、热插拔闪存、Infiniband 网络、RDMA 远程直接存取数据的数据库解决方案,为用户提供高可用、易扩展、高性能的数据库服务,适用于 OLAP、 OLTP 以及混合负载等各种应用场景下的极限性能需求,支持 Oracle、SQL Server、MySQL 和 PostgreSQL 等各种主流数据库。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档