以VueLeaflet为中心,查看多个图层的所有geoJson几何图形。

问题描述 投票:0回答: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图层,并尽可能地放大?

javascript vue.js leaflet geojson vue2leaflet
1个回答
0
投票

我设法解决了这个问题。这里的代码。

//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] });
});
© www.soinside.com 2019 - 2024. All rights reserved.