Leaflet 自定义Marker点Icon

Leaflet 自定义Marker点Icon

Leaflet 支持自定义Icon,我们可以给Marker点添加自定义的Icon。

创建自定义Icon

 var greenIcon = L.icon({
                iconUrl:'/assets/maker/marker-icon.png',//icon阴影图片路径
                shadowUrl:'/assets/maker/marker-shadow.png',//icon阴影图片路径

                iconSize:     [16, 32], // size of the icon
                shadowSize:   [16, 32], // size of the shadow
                iconAnchor:   [0, 16], // point of the icon which will correspond to marker's location
                shadowAnchor: [0, 0],  // the same for the shadow
                popupAnchor:  [-0, -32] // point from which the popup should open relative to the iconAnchor
                });

修改默认图标路径

Leaflet不仅支持直接使用自定义Icon,还可以修改系统默认Icon,但是只支持修改一整套,包括 icon和shadow。

直接把自己的命名为marker-icon.png和marker-shadow.png图片放在一个目录下,然后把目录的地址赋值给L.Icon.Default.imagePath

代码

 L.Icon.Default.imagePath ='../assets/maker/'  

加载Marker

L.marker([x,y],options);

options里面可以添加自定义的Icon对象:

例如:

{Icon":greenIcon}

整个样例源码:


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

<script>
export default {   
//生命周期 - 挂载完成(访问DOM元素)
    mounted() {
        this.init();
    },
    methods:{
        init(){
            let map=L.map('map',{
                center:[30.33, 120.10015131],
                zoom:13
            })
            //L.Icon.Default.imagePath ='../../assets/maker/' //修改默认Icon
            var greenIcon = L.icon({
                iconUrl:'/assets/maker/marker-icon.png',
                shadowUrl:'/assets/maker/marker-shadow.png',

                iconSize:     [16, 32], // 设置icon大小
                shadowSize:   [16, 32], // 设置shadow大小
                iconAnchor:   [0, 16], // point of the icon which will correspond to marker's location
                shadowAnchor: [0, 0],  // the same for the shadow
                popupAnchor:  [-0, -32] // point from which the popup should open relative to the iconAnchor
                });
            //加载图层
            L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
            }).addTo(map);
            //加载marker点
            L.marker([30.33, 120.10015131],{icon:greenIcon}).addTo(map)
            .bindPopup('A pretty CSS3 popup.<br> Easily customizable.')
            .openPopup();
        }
    }
}
</script>
<style scoped>
#map{
    height: 800px;
    width: 1200px;
}
</style>

效果:

image-20201013183951309

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值