uniapp使用map组件绑定点击地图事件并获取点击位置的经纬度
在uniapp中使用map组件绑定点击地图事件并获取点击位置的经纬度可以按照以下步骤进行:
1.在template中添加map组件,并绑定tap事件:
<template>
<view>
<map :longitude="longitude" :latitude="latitude" @tap="tapMap"></map>
</view>
</template>
2.在script中定义tapMap方法,通过event参数获取点击位置的经纬度:
<script>
export default {
data() {
return {
longitude: 113.324520,
latitude: 23.099994
}
},
methods: {
tapMap(event) {
console.log(event)
console.log(event.detail.latitude)
console.log(event.detail.longitude)
}
}
}
</script>
在上面的示例中,tapMap方法会在地图上点击时触发,并通过event参数获取点击位置的经纬度。其中,event.detail.latitude和event.detail.longitude分别表示点击位置的纬度和经度。
注意:需要在manifest.json文件中声明使用地图组件的权限,如下所示:
"mp-weixin": {
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
}
}