Leaflet JS + Leaflet.Deflate - Changing default ma

2019-09-03 12:22发布

问题:

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.

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:

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?

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>

回答1:

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

The answer is: No.

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

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.