【问题标题】:Center VueLeaflet view to all geoJson geometries from multiple layers将 VueLeaflet 视图集中到来自多个图层的所有 geoJson 几何图形
【发布时间】:2020-06-02 19:22:27
【问题描述】:

我的地图上有一个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】:

    我设法解决了。代码如下:

    //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] });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 2013-07-19
      相关资源
      最近更新 更多