uniapp小程序地图重置视角

记载了项目开发过程中map组件如何重置视角

文章目录


前言

本文主要记载了小程序map组件如何重置视角


一、问题描述

uniapp根据longitude、latitude、scale分别设置经纬度、缩放,但是比较坑的一点是只有更改上述值时才会触发重置视角的效果,并且手动拖动或者缩放并不算更改上述值。

二、步骤

1.标签容器

代码如下:

<map id="map" :longitude="longitude" :latitude="latitude" :scale="scale" themt="satellite"></map>

2.定义变量及事件

在data中定义几个变量:

data() {
    return {
      longitude: 116.432934,
      latitude: 39.8427,
      scale: 10,
      // 固定的经纬度,为了重置视图
      fixLongitude: undefined,
      fixLatitude: undefined,
      fixScale: undefined,
      mapContext: undefined,
    }
  },

 官网原文:创建并返回 map 上下文 mapContext 对象:

onReady() {
    this.mapContext = uni.createMapContext('map', this)
  },

给按钮绑定重置视角的事件:
 

resetView() {
      this.mapContext.getScale({
        success: res => {
          const scale = this.fixScale
          this.scale = res.scale;
          this.$nextTick(() => {
            this.scale = scale
          })
        }
      })
      this.mapContext.getCenterLocation({
        success: res => {
          const fixPos = [this.fixLongitude, this.fixLatitude]
          this.longitude = res.longitude
          this.latitude = res.latitude
          this.$nextTick(() => {
            this.longitude = fixPos[0]
            this.latitude = fixPos[1]
          })
        }
      })
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值