Leaflet JS + Leaflet.Deflate-将默认标记图标更改为自定义图标 [英] Leaflet JS + Leaflet.Deflate - Changing default marker icon to custom icon

查看:201
本文介绍了Leaflet JS + Leaflet.Deflate-将默认标记图标更改为自定义图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我之前的帖子' Leaflet JS -在特定缩放级别上将esri形状更改为标记 '我能够解决传单JS 库和更改多边形形状达到特定缩放级别时,会显示标记图标.

In my previous post 'Leaflet JS - changing esri shape into marker on certain zoom level ' I was able to resolve an issue which i had with the leaflet JS library and changing the polygon shapes to markers icons when hitting a certain zoom level.

'Ivan Sanchez'建议我使用' Leaflet.Deflate 插件而且它的工作原理就像是一种魅力,因此,在经过一定的缩放级别后,现在各种形状都已转换为标记,但是现在我正努力将默认传单标记图标更改为自定义标记图标,所以我的问题是:

I was advised by 'Ivan Sanchez' to use the 'Leaflet.Deflate' plugin and this works like a charm, so now the various shapes are being transformed into markers after a certain zoomlevel, however now I'm struggling to change the default leaflet marker icon to a custom marker icon, so my question now is:

是否可以在使用" Leaflet.ShapeFile '和'

Is it possible to change the default marker icon to a custom marker icon while using the 'Leaflet.ShapeFile' and 'Leaflet.Deflate' plugin and what would be the best approach to do this?

我想制作一个JSFiddle,但我不允许JSFiddle允许我附加包含shapefile的zip文件,因此,我将在下面发布到目前为止获得的代码,感谢您的帮助,建议和支持:

I wanted to make a JSFiddle, but I don't JSFiddle allows me to attach the zip file contains the shapefiles, so I will post the code I have got so far below here, thanks for your help, advise and support:

<!doctype html>
<html lang="en">

<head>
    <meta charset='utf-8' />
    <title>v4</title>
    <link rel="stylesheet" type="text/css" href="lib/leaflet/leaflet.css" />
    <!--[if lte IE 8]> <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.ie.css" /> <![endif]-->
<link rel="stylesheet" type="text/css" href="lib/leaflet/L.Control.Sidebar.css" />
<style>
    html { height: 100% }
    body { height: 100%; margin: 0; padding: 0; }
    #map { height: 100% }
</style>
</head>

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

    <script src="lib/jquery/jquery-3.1.1.min.js"></script>
    <script src="lib/leaflet/leaflet.js"></script>
    <script src="lib/leaflet/catiline.js"></script>
    <script src="lib/leaflet/leaflet.shpfile.js"></script>
    <script src="lib/leaflet/shp.js"></script>
    <script src="lib/leaflet/L.Control.Sidebar.js"></script>
    <script src="lib/leaflet/L.Deflate.js"></script>

    <script>
        // init map
        var m = L.map('map').setView([52.472833, 1.749609], 15);

        // clicking on the map will hide the sidebar plugin.
        m.on('click', function () {
            sidebar.hide();
        });

        // init Deflate plugin
        L.Deflate({ minSize: 10 }).addTo(m);

        // Init side bar control
        var sidebar = L.control.sidebar('sidebar', { closeButton: true, position: 'right' });
        m.addControl(sidebar);

        // Init esri shape file via leaflet.shapefile, shp.js plugin
        var businessProperties = new L.Shapefile('data/businessshapes.zip', { style: propertyStyle, onEachFeature: propertyOnEachFeature }).addTo(m);

        // create on-click Feature
        function propertyOnEachFeature(feature, layer) {
            layer.on( {
                mouseover: highlightFeature,
                mouseout: resetHighlight,
                click: function populate() {
                    sidebar.toggle();
                    document.getElementById('pinfoHeader').innerHTML = "<h2>" + feature.properties.Building + " - Detailed Information</h2><br />";
                    document.getElementById('pTitle').innerHTML = "Name: " + feature.properties.Building
                    document.getElementById('pDetails').innerHTML = "SHAPE_Leng: " + feature.properties.SHAPE_Leng + "<br/ >SHAPE_Area: " + feature.properties.SHAPE_Area
                }, highlightFeature, zoomToFeature
            });
        }

        // style the properties style
        function propertyStyle(feature) {
            return {
                fillColor: getPropertyColor(feature.properties.BusType),
                weight: 2,
                opacity: 1,
                color: 'white',
                dashArray: 3,
                fillOpacity: 0.7
            }
        }

        // set color per property according to the data table of the Esri Shape file.
        function getPropertyColor(propStatus) {
            if (propStatus == 'TypeA') {
                return 'red';
            } else if (propStatus == 'TypeB') {
                return 'green';
            } else {
                return 'yellow';
            }
        }

        // set the highlighted color for polygon
        function highlightFeature(e) {
            var layer = e.target;
            layer.setStyle( {
                weight: 2,
                color: 'black',
                fillColor: 'white',
                fillOpacity: 0.2
            });

            if (!L.Browser.ie && !L.Browser.opera) {
                layer.bringToFront();
            }
        }

        // reset the highlighted color for polygon after mouse leave polygon
        function resetHighlight(e) {
            businessProperties.resetStyle(e.target);
        }


        //Extend the Default marker class to overwrite the leaflet.deflate marker icon???
        var TestIcon = L.Icon.Default.extend({
            options: {
                iconUrl: 'assets/images/markers/business.png'
            }
        });
        var testIcon = new TestIcon();

        businessProperties.addTo(m);

        // Init base maps for switch
        var grayscale = L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', { id: 'MapID', attribution: 'Map maintained by <a href="http://www.demo.com">Demo LTD</a>, &mdash; Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a>,' }).addTo(m);
        var streets = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { id: 'MapID', attribution: 'Map maintained by <a href="http://www.demo.com">Demo LTD</a>, &mdash; Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a>,' });

        var baseMaps = {
            "Streets": streets,
            "Grayscale": grayscale
        };

        // Init overlay map switch
        var overlayMaps = {
            "Bussines Properties": businessProperties
        };

        // Add switches to map control
        L.control.layers(baseMaps, overlayMaps).addTo(m);
    </script>
</body>
</html>

推荐答案

使用"Leaflet.Deflate"插件时是否可以将默认标记图标更改为自定义标记图标?

Is it possible to change the default marker icon to a custom marker icon while using the 'Leaflet.Deflate' plugin?

答案是:.

Leaflet.Deflate的当前代码仅使用默认标记和默认标记,请参见

The current code for Leaflet.Deflate uses a default marker and a default marker only, see https://github.com/oliverroick/Leaflet.Deflate/blob/991f51ca82e7bb14a17c8d769b4c378c4ebaf700/src/L.Deflate.js#L42

您可以自行解决,但我建议您在Leaflet.Deflate存储库中提出功能请求.应该可以修改Leaflet.Deflate存储库,以使线/面要素具有一些额外的属性以用作标记选项.

You could hack your way around it, but I would rather recommend filing a feature request in the Leaflet.Deflate repo. It should be possible to modify the Leaflet.Deflate repo to allow line/polygon features to have some extra properties to be used as marker options.

这篇关于Leaflet JS + Leaflet.Deflate-将默认标记图标更改为自定义图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆