谷歌地图Google JS API 实现

demo实现

实现源码👇

// 谷歌地图Google JS API 实现
<template>
  <div class="myMap">
    <gmp-map :center="center" zoom="15" map-id="地图自定义id">
      <gmp-advanced-marker
        v-for="(res, index) in myLatlng"
        :key="index"
        :position="`${res.latitude},${res.longitude}`"
        :title="res.name"
        :content="res.address"
        gmp-click
        @touchend="clickFn($event, res)"
      >
        <img
          class="flag-icon"
          :src="
            require(`@/assets/images/helpSupport/agent/${
              center == `${res.latitude},${res.longitude}` ? 'point2' : 'point1'
            }.png`)
          "
        />
      </gmp-advanced-marker>
    </gmp-map>
  </div>
</template>

<script>
import loadScript from "@/utils/loadScript.js";
export default {
  props: {
    myLatlng: {
      type: Array,
      default: () => [],
    },
    currentIndex: {
      type: Number,
      default: 0,
    },
    center: {
      type: String,
      required: true,
    },
  },
  watch: {
    myLatlng: {
      handler(newDate) {
        if (newDate.length > 0) {
          // window.initMap();
        }
      },
    },
    currentIndex: {
      handler(index) {
        this.center = `${this.myLatlng[index].latitude},${this.myLatlng[index].longitude}`;
        console.log(index, this.myLatlng[index]);
      },
    },
  },
  computed: {
    getLocation() {
      return window.navigator.geolocation;
    },
  },
  data() {
    return {
      infoWindow: null,
    };
  },
  mounted() {},
  async created() {
    let key = "你申请的key";
    await loadScript(
      `https://maps.googleapis.com/maps/api/js?&key=${key}&loading=async&libraries=marker&v=beta&solution_channel=GMP_CCS_complexmarkers_v3`
    );
  },
  methods: {
    clickFn(e, res) {
      console.log(e, e.target.offsetParent);
      const contentString = ` <div class='maptotip' style=" display: flex;flex-direction:             column;align-items: flex-start;justify-content: center;color:#333333;font-weight: 400;text-align: left;">
                                    <p style=" font-family: OPPOSans;font-size: 20px;color: #21A56C;">${res.name}</p>
                                    <p style=" font-family: OPPOSans;font-size: 18px;">${res.address}</p>
                                    <p style=" font-family: OPPOSans;font-size: 18px;color: #666666;">电话:${res.contractPhone}</p>
                              </div > `;
      this.infoWindow = new google.maps.InfoWindow({
        content: contentString,
        ariaLabel: res.name,
      });
      this.infoWindow.open({ anchor: e.target.offsetParent });
    },
  },
};
</script>
<style lang="less">
.myMap {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.flag-icon {
  width: 35px;
  height: 45px;
}

.maptotip {
  width: 200px;
  height: auto;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 5px;
  background-color: rgba(243, 249, 254, 0.9);
  > p {
    &:nth-child(1) {
      font-family: OPPOSans;
      font-size: 32px;
      font-weight: 400;
      line-height: 42.21px;
      text-align: left;
      color: #333333;
    }
    &:nth-child(2) {
      font-family: OPPOSans;
      font-size: 26px;
      font-weight: 400;
      line-height: 34.29px;
      text-align: left;
      color: #333333;
    }
    &:nth-child(3) {
      font-family: OPPOSans;
      font-size: 24px;
      font-weight: 400;
      line-height: 31.66px;
      text-align: left;
      color: #666666;
    }
  }
}
</style>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值