• Vue 腾讯地图 前端定位组件 实现地图定位


    参考网址:

    https://lbs.qq.com/service/webService/webServiceGuide/webServiceOverview

    https://blog.csdn.net/l13620804253/article/details/117259244

    https://www.jianshu.com/p/d331f3449a78

    1、腾讯位置服务申请密钥

    2、vue项目index.html引入

    <script src="https://map.qq.com/api/gljs?v=1.exp&key=申请的密钥"></script>

    3、引入jsonp

    1)安装vue-jsonp:

    cnpm install vue-jsonp --save

    2)在main.js中导入

    import { VueJsonp } from 'vue-jsonp'
    Vue.use(VueJsonp)

    4、封装组件

    <template>
      <div class="Map">
        <div class="search_Map">
          <input type="text" v-model="value" @input="search(value)" />
          <div class="content">
            <p v-for="(i, index) in addressList" :key="index" @click="select(i)">
              {{ i.title }}
              <span class="address">{{ i.address }}</span>
            </p>
          </div>
        </div>
        <div id="container"></div>
      </div>
    </template>
    
    <script>
    // https://lbs.qq.com/webApi/component/componentGuide/componentGeolocation
    export default {
      name: "TXMap", //腾讯地图
      props: {
        latitude: {
          type: [Number, String],
          required: true,
          default: "",
        },
        longitude: {
          type: [Number, String],
          required: true,
          default: "",
        },
      },
      data() {
        return {
          value: "", //搜索内容
          addressList: [], //搜索结果
          selectValue: {}, //选择的某个结果
          timeout: null, //搜索定时器
          map: null,
          marker: null,
        };
      },
      mounted() {
        this.initMap();
      },
      methods: {
        //初始化地图
        initMap() {
          var center = new TMap.LatLng(this.latitude, this.longitude);
          //初始化地图
          this.map = new TMap.Map("container", {
            rotation: 20, //设置地图旋转角度
            pitch: 0, //设置俯仰角度(0~45)
            zoom: 16, //设置地图缩放级别
            center: center, //设置地图中心点坐标
          });
        },
        //地址搜索
        search(value) {
          this.addressList = [];
          clearTimeout(this.timeout);
          if (value) {
            let that = this;
            this.timeout = setTimeout(() => {
              that
                .$jsonp("https://apis.map.qq.com/ws/place/v1/suggestion/", {
                  key: "申请的密钥",
                  output: "jsonp",
                  keyword: value,
                })
                .then((res) => {
                  console.log(res);
                  if (res.status === 0 && res.data) {
                    that.addressList = res.data;
                  }
                })
                .catch((e) => {
                  console.log(e);
                });
            }, 500);
          }
        },
        //选择对应搜索地址
        select(row) {
          console.log(">>>select:", row);
          this.$emit("addressInfo", row);
          this.selectValue = row.location;
          this.addressList = [];
          let self = this;
    
          //更新地图中心位置
          self.map.setCenter(
            new TMap.LatLng(this.selectValue.lat, this.selectValue.lng)
          );
    
          //判断是否存在标记点,有的话清空
          if (self.marker) {
            self.marker.setMap(null);
            self.marker = null;
          }
    
          //初始化marker
          self.marker = new TMap.MultiMarker({
            id: "marker-layer", //图层id
            map: self.map,
            styles: {
              //点标注的相关样式
              marker: new TMap.MarkerStyle({
                 25,
                height: 35,
                anchor: { x: 16, y: 32 },
                src: "https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/markerDefault.png",
              }),
            },
            geometries: [
              {
                //点标注数据数组
                id: "demo",
                styleId: "marker",
                position: new TMap.LatLng(
                  self.selectValue.lat,
                  self.selectValue.lng
                ),
                properties: {
                  title: "marker",
                },
              },
            ],
          });
        },
      },
    };
    </script>
    
    <style lang="scss" scoped>
    .Map {
      position: relative;
      margin-bottom: 10px;
      .search_Map {
        position: absolute;
        left: 20px;
        top: 20px;
        z-index: 99009;
        input {
          border: 1px solid #f1f1f1;
          display: inline-block;
           400px;
          height: 40px;
          padding: 10px;
          color: #5a5a5a;
          background: rgba(255, 255, 255, 0.904);
        }
        .content {
           400px;
          background: rgba(252, 250, 250, 0.918);
          border: 1px solid #f1f1f1;
          border-top: none;
          font-size: 13px;
          color: #5a5a5a;
          max-height: 350px;
          overflow-y: auto;
          p {
            display: inline-block;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
             380px;
            border-bottom: 1px solid #f1f1f1;
            padding: 15px 10px;
            margin: 0;
            cursor: pointer;
            &:hover {
              background: #eff6fd;
            }
            .address {
              font-size: 12px;
              color: #b9b9b9;
              margin-left: 20px;
            }
          }
        }
      }
    }
    #container {
      min- 800px;
       100%;
      height: 400px;
    }
    </style>

    4、使用

        <TXMap
          :latitude="latitude"
          :longitude="longitude"
          @addressInfo="addressInfo"
        />
    
    
    import TXMap from "@/components/TXMap";
  • 相关阅读:
    Java static keyword
    Final Keyword In Java
    Underscore template
    Query.extend() 函数详解-转载
    js闭包for循环总是只执行最后一个值得解决方法
    mui scroll和上拉加载/下拉刷新
    mui 手势事件配置
    118. 杨辉三角
    [ 周赛总结 ] 第 185 场力扣周赛
    55. 跳跃游戏
  • 原文地址:https://www.cnblogs.com/zhaomeizi/p/15335410.html
Copyright © 2020-2023  润新知