zoukankan      html  css  js  c++  java
  • uniapp map地图 拖动页面后点击按钮返回当前位置

    方法一:通过controls实现

    使用controls在地图上显示一个btn图标,点击时调用接口回到当前位置:

    <template>
      <view>
        <map
          id="around-food-map"
          style=" 100vw; height: 100vh;"
          :latitude="latitude"
          :longitude="longitude"
          :controls="controls"
          @controltap="onControltap"
        ></map>
      </view>
    </template>
    
    <script>
    export default {
      data() {
        return {
          latitude: 39.909, // 初始定在首都
          longitude: 116.39742,
          controls: [
            {
              //在地图上显示控件,控件不随着地图移动
              id: 0, //控件id
              iconPath: "../../static/current-location.png", //显示的图标
              clickable: true,
              position: {
                //控件在地图的位置
                left: 200,
                top: 200,
                 60,
                height: 60,
              },
            },
          ],
        };
      },/*  */
      methods: {
        onControltap(control) {
          uni.createMapContext("around-food-map", this).moveToLocation({
            longitude: this.longitude,
            latitude: this.latitude,
          });
        },
      },
    };
    </script>
    
    

    不过微信官网说controls即将废弃,建议使用 cover-view 代替,所以下面使用 cover-view

    方法二:使用 cover-view 实现

    <template>
      <view class="map-container">
        <map
          id="around-company-map"
          style=" 100vw; height: 100vh;"
          :latitude="latitude"
          :longitude="longitude"
        >
          <cover-view class="cover-view" @click="onControltap"></cover-view>
        </map>
      </view>
    </template>
    
    <script>
    export default {
      data() {
        return {
          /* 地图 */
          id: 0, // 使用 marker点击事件 需要填写id
          latitude: 39.909, // 默认定在首都
          longitude: 116.39742,
        };
      },
      methods: {
        onControltap(control) {
          uni.createMapContext("around-company-map", this).moveToLocation({
            longitude: this.longitude,
            latitude: this.latitude,
          });
        },
      },
    };
    </script>
    
    <style lang="less">
    @import "~@/styles/global.less";
    .map-container {
      position: relative;
      height: 100vh;
      overflow: hidden;
      .cover-view {
         60px;
        height: 60px;
        border-radius: 50%;
        background-image: url("~@/static/current-location.png");
        background-size: 60px 60px;
        background-position: center center;
        position: absolute;
        bottom: 24px;
        right: 16px;
      }
    }
    </style>
    
  • 相关阅读:
    API函数
    平台调用教程
    查看网页源文件方法
    网页端商品链接转换为手机端链接的部分网址规则
    中文分词消除歧义简单思想
    java 链接数据库时的配置代码
    手机参数更新语句根据Id 可以得到某手机的各种参数
    中文分词—基于Lucene的分词器—支持中英文混合词
    修改Imdict做自己的分词器
    制作可输入的下拉框
  • 原文地址:https://www.cnblogs.com/XHappyness/p/13524179.html
Copyright © 2011-2022 走看看