【箩筐地图】vue项目中使用箩筐地图(web端),对点进行icon标记,点击标记弹窗显示点位信息

1.注册账号申请key:https://lbs.luokuang.com/doc.html#/doc/jsapi/devGuide/intro/prepare

2.如何把地图的key引入项目

在public目录下找到index.html
<script src="https://webapi.luokuang.com/maps?ak=您申请的ak"></script>

3.添加地图容器

<div id="map"></div>

4.初始化数据

data() {
    return {
      map: null,
      inforWindow: null,
      lnglats: [
        {
          id: 1,
          name: "动物园",
          position: [116.33731593367531, 39.941387955338996],
        },
        {
          id: 2,
          name: "玉渊谭公园",
          position: [116.31959602683907, 39.916069734293075],
        },
        {
          id: 3,
          name: "北京天安门",
          position: [116.3984929272525, 39.90787656038206],
        },
        {
          id: 4,
          name: "日坛公园",
          position: [116.44389016057424, 39.914487034587154],
        },
      ],
    };
  },

5.添加标记到地图

在这里插入图片描述

/**
     * 添加标记到地图上
     */

    addMarkers() {
      let position = new LKMap.LngLat(116.39751541617153, 39.90768542687843);
      let markers = [];
      this.map = new LKMap.Map("map", {
        center: position,
        zoom: 10.5,
      });

      //使用自定义的icon的图标
      // const icon = new LKMap.Icon({
      //   size: new LKMap.Size(44, 58),
      //   image: require("../../assets/dataOverflow/marker.png"),
      //   imageSize: new LKMap.Size(44, 58),
      //   imageOffset: new LKMap.Pixel(0, 0),
      // });

      for (let i = 0; i < this.lnglats.length; i++) {
        markers[i] = new LKMap.Marker({
          map: this.map,
          position: new LKMap.LngLat(
            this.lnglats[i].position[0],
            this.lnglats[i].position[1]
          ),
          anchor: "top",
          extData: {
            id: i,
            name: this.lnglats[i].name,
          },
          // icon: icon  //使用自定义的icon
        });
        markers[i].on("click", this.markerClick);
      }
    },

6.点击弹窗事件

在这里插入图片描述

markerClick(e) {
      const extData = e.layer.getExtData();
      const title = "";
      const content = [
        `<img src='https://lkimgyt.luokuang.com/1587364867424.jpg'><span>${extData.name}</span>`,
        `<label>地址: 宣外大街8号庄胜崇光百货南馆</label>`,
        `<label>电话: 13888888888</label>`,
        `<label>营业时间: 10:00-23:00</label>`
      ].join("<br/>");
      this.inforWindow = new LKMap.InforWindow({
        anchor: "bottom",
        className: "customClassName",
        content: this.createInfoWindow(title, content),
        isCustom: true, //使用自定义窗体
        offset: new LKMap.Pixel(0, -16), // 设置信息窗体偏移量
      });

      const markerPosition = e.layer.getPosition();
      this.inforWindow.open(this.map, markerPosition);
    },
    createInfoWindow(title, content) {
      const info = document.createElement("div");
      info.className = "custom-info content-window-card";
      const top = document.createElement("div");
      const titleD = document.createElement("div");
      const closeX = document.createElement("img");
      top.className = "info-top";
      titleD.innerHTML = title;
      closeX.src = "https://lkimgyt.luokuang.com/1587366541654.png";
      closeX.onclick = this.closeInfoWindow.bind(this);
      top.appendChild(titleD);
      top.appendChild(closeX);
      info.appendChild(top);
      // 定义中部内容
      const middle = document.createElement("div");
      middle.className = "info-middle";
      middle.style.backgroundColor = "white";
      middle.innerHTML = content;
      info.appendChild(middle);
      // 定义底部内容
      const bottom = document.createElement("div");
      bottom.className = "info-bottom";
      bottom.style.position = "relative";
      bottom.style.top = "-3px";
      bottom.style.margin = "0 auto";
      const sharp = document.createElement("img");
      sharp.src = "https://lkimgyt.luokuang.com/1587367651134.png";
      bottom.appendChild(sharp);
      info.appendChild(bottom);
      return info;
    },
    closeInfoWindow() {
      this.map.clearInfoWindow();
    },

7.弹窗相关样式

<style>
#map {
  width: 100%;
  height: 100%;
}
.content-window-card {
  position: relative;
  bottom: 0;
  left: 0;
  width: 400px;
  height: 120px;
  padding: 0;
  border: 0;
  background: 0;
}
.content-window-card p {
  height: 2rem;
}
div.info-top {
  position: relative;
  height: 0;
}
div.info-top div {
  display: inline-block;
  color: #333333;
  font-size: 14px;
  font-weight: bold;
  line-height: 31px;
  padding: 0 10px;
}
div.info-top img {
  position: absolute;
  top: 10px;
  right: 10px;
  transition-duration: 0.25s;
}
div.info-top img:hover {
  box-shadow: 0px 0px 5px #000;
}
div.info-middle {
  font-size: 12px;
  padding: 8px;
  line-height: 20px;
  background: rgba(255, 255, 255, 1);
  box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.08);
  border-radius: 4px;
  -webkit-border-radius: 4px;
  width: 380px;
  height: 108px;
}
div.info-middle span {
  font-size: 16px;
  font-weight: bold;
  color: rgba(51, 51, 51, 1);
  display: inline-block;
  line-height: 30px;
}
div.info-middle label {
  display: inline-block;
  line-height: 22px;
  padding-left: 4px;
  margin-bottom: 0;
}
div.info-middle label.price {
  color: rgba(186, 51, 48, 1);
}
div.info-middle a {
  float: right;
  color: #0037e4;
}
div.info-bottom {
  height: 0px;
  width: 100%;
  clear: both;
  text-align: center;
  background: 0;
}
div.info-bottom img {
  position: relative;
  z-index: 104;
}
span {
  margin-left: 5px;
  font-size: 11px;
}
.info-middle img {
  float: left;
  margin-right: 6px;
}
</style>

更多官网案例:https://lbs.luokuang.com/jsdemo/map/index.html?keywords=marker-several

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值