Hachiman
Hachiman
  • 发布:2025-03-25 09:46
  • 更新:2025-05-22 10:37
  • 阅读:124

【报Bug】在App端获取map组件的context后调用getCenterLocation,getRegion,getScale获取不到返回值 在h5端可以获取到

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win11

HBuilderX类型: 正式

HBuilderX版本号: 4.55

手机系统: Android

手机系统版本号: Android 10

手机厂商: vivo

手机机型: s7

页面类型: vue

vue版本: vue3

打包方式: 云端

项目创建方式: HBuilderX

示例代码:

<template>
<map id="mapid" class="map1" ref="map1" scale="16" @tap="tapHandler" @callouttap="markertapHandler" @regionchange="regionchangeHandler" :polygons="props.location" :latitude="props.latitude" :longitude="props.longitude" :markers="props.avgMarkers"></map>
</template>
<script setup>
import { ref, defineProps, onMounted, getCurrentInstance } from 'vue'

const props = defineProps({
polygons: Array,
latitude: Number,
longitude: Number,
markers: Array
})

const con = ref()
const instance = ref()

const ids = ref(666)

onMounted(() => {
var pages = getCurrentPages()
var page = pages[pages.length - 1]
instance.value = getCurrentInstance()

// #ifdef APP-PLUS
var currentWebview = page.$getAppWebview()
console.log(currentWebview.id) //获得当前webview的id
console.log(currentWebview.isVisible()) //查询当前webview是否可见
con.value = uni.createMapContext('mapid', instance.value)
console.log(con.value, 'concon')

// #endif
})

const markertapHandler = e => {}
const regionchangeHandler = e => {}

const tapHandler = e => {
console.log('first')
con.value.getScale({
success: function (res) {
console.log(res, '执行了吗')
},
fail: function (res) {
console.log(res, '失败了')
}
})
}
</script>
<style lang="scss" scoped>
.map1 {
width: 750rpx;
height: 1000rpx;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>

操作步骤:
<template>  
  <map id="mapid" class="map1" ref="map1" scale="16" @tap="tapHandler" @callouttap="markertapHandler" @regionchange="regionchangeHandler" :polygons="props.location" :latitude="props.latitude" :longitude="props.longitude" :markers="props.avgMarkers"></map>  
</template>  
<script setup>  
import { ref, defineProps, onMounted, getCurrentInstance } from 'vue'  

const props = defineProps({  
  polygons: Array,  
  latitude: Number,  
  longitude: Number,  
  markers: Array  
})  

const con = ref()  
const instance = ref()  

const ids = ref(666)  

onMounted(() => {  
  var pages = getCurrentPages()  
  var page = pages[pages.length - 1]  
  instance.value = getCurrentInstance()  

  // #ifdef APP-PLUS  
  var currentWebview = page.$getAppWebview()  
  console.log(currentWebview.id) //获得当前webview的id  
  console.log(currentWebview.isVisible()) //查询当前webview是否可见  
  con.value = uni.createMapContext('mapid', instance.value)  
  console.log(con.value, 'concon')  

  // #endif  
})  

const markertapHandler = e => {}  
const regionchangeHandler = e => {}  

const tapHandler = e => {  
  console.log('first')  
  con.value.getScale({  
    success: function (res) {  
      console.log(res, '执行了吗')  
    },  
    fail: function (res) {  
      console.log(res, '失败了')  
    }  
  })  
}  
</script>  
<style lang="scss" scoped>  
.map1 {  
  width: 750rpx;  
  height: 1000rpx;  
  position: fixed;  
  top: 0;  
  bottom: 0;  
  left: 0;  
  right: 0;  
}  
</style>  

预期结果:
<template>  
  <map id="mapid" class="map1" ref="map1" scale="16" @tap="tapHandler" @callouttap="markertapHandler" @regionchange="regionchangeHandler" :polygons="props.location" :latitude="props.latitude" :longitude="props.longitude" :markers="props.avgMarkers"></map>  
</template>  
<script setup>  
import { ref, defineProps, onMounted, getCurrentInstance } from 'vue'  

const props = defineProps({  
  polygons: Array,  
  latitude: Number,  
  longitude: Number,  
  markers: Array  
})  

const con = ref()  
const instance = ref()  

const ids = ref(666)  

onMounted(() => {  
  var pages = getCurrentPages()  
  var page = pages[pages.length - 1]  
  instance.value = getCurrentInstance()  

  // #ifdef APP-PLUS  
  var currentWebview = page.$getAppWebview()  
  console.log(currentWebview.id) //获得当前webview的id  
  console.log(currentWebview.isVisible()) //查询当前webview是否可见  
  con.value = uni.createMapContext('mapid', instance.value)  
  console.log(con.value, 'concon')  

  // #endif  
})  

const markertapHandler = e => {}  
const regionchangeHandler = e => {}  

const tapHandler = e => {  
  console.log('first')  
  con.value.getScale({  
    success: function (res) {  
      console.log(res, '执行了吗')  
    },  
    fail: function (res) {  
      console.log(res, '失败了')  
    }  
  })  
}  
</script>  
<style lang="scss" scoped>  
.map1 {  
  width: 750rpx;  
  height: 1000rpx;  
  position: fixed;  
  top: 0;  
  bottom: 0;  
  left: 0;  
  right: 0;  
}  
</style>  

实际结果:
<template>  
  <map id="mapid" class="map1" ref="map1" scale="16" @tap="tapHandler" @callouttap="markertapHandler" @regionchange="regionchangeHandler" :polygons="props.location" :latitude="props.latitude" :longitude="props.longitude" :markers="props.avgMarkers"></map>  
</template>  
<script setup>  
import { ref, defineProps, onMounted, getCurrentInstance } from 'vue'  

const props = defineProps({  
  polygons: Array,  
  latitude: Number,  
  longitude: Number,  
  markers: Array  
})  

const con = ref()  
const instance = ref()  

const ids = ref(666)  

onMounted(() => {  
  var pages = getCurrentPages()  
  var page = pages[pages.length - 1]  
  instance.value = getCurrentInstance()  

  // #ifdef APP-PLUS  
  var currentWebview = page.$getAppWebview()  
  console.log(currentWebview.id) //获得当前webview的id  
  console.log(currentWebview.isVisible()) //查询当前webview是否可见  
  con.value = uni.createMapContext('mapid', instance.value)  
  console.log(con.value, 'concon')  

  // #endif  
})  

const markertapHandler = e => {}  
const regionchangeHandler = e => {}  

const tapHandler = e => {  
  console.log('first')  
  con.value.getScale({  
    success: function (res) {  
      console.log(res, '执行了吗')  
    },  
    fail: function (res) {  
      console.log(res, '失败了')  
    }  
  })  
}  
</script>  
<style lang="scss" scoped>  
.map1 {  
  width: 750rpx;  
  height: 1000rpx;  
  position: fixed;  
  top: 0;  
  bottom: 0;  
  left: 0;  
  right: 0;  
}  
</style>  

bug描述:

map组件 在使用uni.createMapContext('mapid') 获取map的context后调用getCenterLocation,getRegion,getScale三个方法都会走成功的success函数但是都没有返回结果 这个在h5端是可以获取的 在真机运行和模拟器运行下都获取不到返回结果为空对象{}

2025-03-25 09:46 负责人:无 分享
已邀请:
1***@qq.com

1***@qq.com

请问楼主有解决的办法吗?我也碰到了一样的问题

要回复问题请先登录注册