0
点赞
收藏
分享

微信扫一扫

Vue工程搭建Leaflet项目第二弹:Marker && Icon展示

干自闭 2022-01-23 阅读 58

前言

1.定义图标

var myIcon = L.icon({
    iconUrl: Icon,
    iconSize: [0, 0],   // 图标尺寸
    iconAnchor: [0, 0], // 图标放置位置
    popupAnchor: [0, 0], // 弹出框偏移量
    shadowUrl: IconShadow,
    shadowSize: [0, 0],
    shadowAnchor: [-2, 0]
});
const marker1 = L.marker(latlng1, { icon: myIcon }).addTo(map);
const marker2 = L.marker(latlng2, { icon: myIcon }).addTo(map);
const marker3 = L.marker(latlng3, { icon: myIcon }).addTo(map);

2.加载Marker

const latlng1 = L.latLng([24.89299, 102.836694]);
const latlng2 = L.latLng([24.85101, 102.795231]);
const latlng3 = L.latLng([24.79112, 102.795231]);
   
const marker1 = L.marker(latlng1, { icon: myIcon }).addTo(map);
const marker2 = L.marker(latlng2, { icon: myIcon }).addTo(map);
const marker3 = L.marker(latlng3, { icon: myIcon }).addTo(map);

3.定义Popup以及ToolTip并添加到地图

let popup1 = L.popup();
let popup2 = L.popup();

// let toolTip = L.tooltip({
//   pane: "tooltipPane"
// });
popup1
    .setLatLng(latlng1)
    .setContent("<p>Hello world1!<br />This is a nice popup.</p>");

popup2
    .setLatLng(latlng2)
    .setContent("<p>Hello world2!<br />This is a nice popup.</p>");

marker1.bindPopup(popup1);
marker2.bindPopup(popup2);

marker3.bindTooltip("my tooltip text").openTooltip();

// popup弹出事件
marker1.on("popupopen", evt => {
    console.log("evtopen:", evt);
});
// popup关闭事件
marker1.on("popupclose", evt => {
    console.log("evtclose:", evt);
});

4.图标偏移在这里插入图片描述

5.图标路径

import Icon from "./marker-icon.png";
import IconShadow from "./marker-shadow.png";

const myIcon = L.icon({
    iconUrl: Icon,
    // iconSize: [40, 40],
    // iconAnchor: [22, 94],
    iconAnchor: [0, 0],
    // popupAnchor: [-3, -76]
    shadowUrl: IconShadow,
    // shadowSize: [40, 40],
    // shadowAnchor: [-2, 0]
    shadowAnchor: [0, 0]
});

6.效果展示

在这里插入图片描述

若大家对进阶打怪有兴趣的话,欢迎订阅以下专栏

举报

相关推荐

0 条评论