1

我的地图上有一个可变的geoJson图层列表(我将其命名为sectors)。我需要将视图居中以适合所有几何图形。

我正在使用Vue2Leaflet (v2.5.2)Leaflet (v1.6.0)

每个geoJson对象都是这样的:

{ 
    "type": "FeatureCollection", 
    "features": 
    [ 
        { 
            "type": "Feature", 
            "properties": 
            {
                "id": "4200051", 
                "name": "Abdon Batista", 
                "description": "Abdon Batista"
            }, 

            "geometry": 
            { 
                "type": "Polygon", 
                "coordinates": [[[-51.0378352721, -27.5044338231], ...]] 
            }
        }
    ]
}

它直接存储在geoSectors: [ geoJson1, geoJson2, ... ].

这是代码:

<template>
    <div class="home">      
        <l-map ref="mapControl" :zoom="zoom" :center="center" :options="mapOptions">
            <l-tile-layer :url="url" :attribution="attribution"/>
            <l-control-zoom position="bottomright"/>

            <!-- Sectors-->
            <l-geo-json v-for="sector in geoSectors" :key="sector.id" :geojson="sector" :options="options"/>
        </l-map>
    </div>
</template>

<script>
    import { latLng, latLngBounds } from "leaflet";

    export default {
        data() {
            return {
                zoom: 6,
                center: latLng(-25.005973, -50.537109),
                url: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
                attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
                mapOptions: {
                    zoomSnap: 0.1,
                    zoomControl: false
                },

                geoSectors: []
            }
        },

        mounted(){
            let list = [];
            //Gets the list from a file.
            this.geoSectors = list;

            var bounds = this.geoSectors; //HERE: Get bounds of the list.
            this.$refs.mapControl.mapObject.fitBounds(bounds);
        },

        computed: {
            options() {
                return {
                    onEachFeature: this.onEachFeatureFunction
                };
            },
            onEachFeatureFunction() {
                return (feature, layer) => {
                    layer.on('click', function (e) {
                        //Shows sector details.
                    });

                    //Tooltips.
                    layer.bindTooltip("<div><p>Sector: "feature.properties.name +"</p></div>", { 
                        permanent: false, 
                        sticky: true 
                    });
                };
            }
        },
    }
</script>

如何使地图视图以最大的缩放级别显示所有 geoJson 图层?

4

1 回答 1

2

我设法解决了它。这是代码:

//You need to import featureGroup from leaflet:
import { latLng, featureGroup } from "leaflet";

//After building your geoJson layers, just call this:
this.$nextTick().then(() => {
    var group = new featureGroup();

    this.$refs.mapControl.mapObject.eachLayer(function(layer) {
        if (layer.feature != undefined)
            group.addLayer(layer);
    });

    this.$refs.mapControl.mapObject.fitBounds(group.getBounds(), { padding: [20, 20] });
});
于 2020-06-03T13:02:45.150 回答