Vue+Leaflet:(二)显示标记、圆、多边形和弹的地图

1.组件:

<template>
  <div>
    <h1>示例一(标记、圆、多边形和弹窗)</h1>
    <div id="map"></div>
  </div>
</template>

<script>
import icon from "leaflet/dist/images/marker-icon.png";
import iconShadow from "leaflet/dist/images/marker-shadow.png";
export default {
  data() {
    return {
      map: null,
      marker: null,
      circle: "",
      polygon: "",
    };
  },
  mounted() {
    this.getMap();
  },
  methods: {
    getMap() {
      // 初始化地图
      this.map = L.map("map").setView([30.89897, 121.921036], 13);
      L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
        maxZoom: 19,
        attribution:
          '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
      }).addTo(this.map);
      // 设置图标
      let DefaultIcon = L.icon({
        iconUrl: icon,
        shadowUrl: iconShadow,
      });
      L.Marker.prototype.options.icon = DefaultIcon;
      // 将图标挂载到地图上
      this.marker = L.marker([30.9099, 121.921036]).addTo(this.map);
      // 创建一个圆
      this.circle = L.circle([30.89965, 121.9363], {
        color: "red",
        fillColor: "#f03",
        fillOpacity: 0.5,
        radius: 1000,
      }).addTo(this.map);
      // 创建多边形
      this.polygon = L.polygon([
        [30.899, 121.98],
        [30.859, 121.96],
        [30.891, 121.9],
      ]).addTo(this.map);
      // 创建弹窗
      this.marker.bindPopup("<b>Hello world!</b><br>I am a popup.").openPopup();
      this.circle.bindPopup("I am a circle.");
      this.polygon.bindPopup("I am a polygon.");
    },
  },
};
</script>

<style scoped>
#map {
  height: 500px;
  width: 1200px;
}
</style>

2.效果图

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值