【问题标题】:How to adjust zoom level in react-leaflet based on the coordinates of the polygons如何根据多边形的坐标调整反应传单中的缩放级别
【发布时间】:2020-11-02 09:58:49
【问题描述】:

我正在尝试使用多边形创建地图。我们如何确定缩放级别,以便所有多边形在地图上可见。

【问题讨论】:

    标签: reactjs dictionary react-leaflet


    【解决方案1】:

    您可以为此使用FeatureGroup。像这样的:

    var mymap = L.map('mapid').setView([51.505, -0.09], 13);
    
    L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
      maxZoom: 18,
      attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
        '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
        'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
      id: 'mapbox/streets-v11',
      tileSize: 512,
      zoomOffset: -1
    }).addTo(mymap);
    
    var poly1 = L.polygon([
      [51.509, -0.08],
      [51.503, -0.06],
      [51.51, -0.047]
    ]).addTo(mymap);
    
    var poly2 = L.polygon([
      [51.519, -0.18],
      [51.513, -0.16],
      [51.52, -0.147]
    ]).addTo(mymap);
    
    var group = L.featureGroup([poly1, poly2]);
    
    mymap.fitBounds(group.getBounds());
    <link href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" rel="stylesheet"/>
    <script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
    <div id="mapid" style="width: 600px; height: 400px;"></div>

    【讨论】:

      猜你喜欢
      • 2012-10-31
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多