代码之家  ›  专栏  ›  技术社区  ›  jcarapia

传单填充不起作用

  •  1
  • jcarapia  · 技术社区  · 7 年前

    mymap.fitBounds(myLayer.getBounds(), {padding: [1000, 1000]})
    

    我认为应该在myLayer获得的边界上添加填充。getBounds(),则映射应根据边界和填充缩放到适当的级别。但是,我看不出是否传入填充选项有什么不同。我也不认为改变填充数字有什么不同。

    JS代码

    //instantiate map
    var mymap = L.map('mapid').setView([39.8283, -98.5795], 3);
    
    //apply layer
    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?
      access_token={accessToken}', {
      attribution: 'Map data &copy; <a 
      href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a 
      href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, 
      Imagery © 
      <a href="http://mapbox.com">Mapbox</a>',
      maxZoom: 18,
      id: 'mapbox.streets',
      accessToken: 'all-your-tokens-are-belong-to-me'
    }).addTo(mymap);
    
    //cities for markers
    var locations ={
      boston: {"type": "Point", "coordinates": [-71.05977, 42.35843]},
      oakland: {"type": "Point", "coordinates": [-122.2708, 37.80437]},
      berkeley: {"type": "Point", "coordinates": [-122.2737, 37.8712]},
      london: {"type": "Polygon", "coordinates": [[[-0.08, 51.509], [-0.06, 
      51.503], [-0.047, 51.51]]]},
      dublin: {"type": "Polygon", "coordinates": [[ [-6.2890, 53.3439], 
      [-6.2653, 
      53.3435], [-6.2603, 53.3356], [-6.2936, 53.3320] ]]},
      glasgow: {"type": "Point", "coordinates": [-4.2435, 55.8627]}
    };
    
    //define styles
    var myStyle = {
      "color": "#ff7800",
      "weight": 5,
      "opacity": 0.65
    };
    
    //Create geoJson layer
    var myLayer = L.geoJSON(null, {style: myStyle}).addTo(mymap);
    
    //function to add element to geoJson layer
    function updateMap(object) {
      myLayer.addData(object);
    };
    
    //click on the location's button, add it to the map
    $('button').click(function(){
      var region = locations[this.id]
      updateMap(region);
      regionTypeContainer.push(region.type);
      mymap.fitBounds(myLayer.getBounds(), {padding: [1000, 1000]}); // padding 
      //not doing anything
    });
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   ghybs    7 年前

    padding 选择 fitBounds() 方法是将地图视口的某些部分从 fitBounds

    如果你试着 fitBounds公司 如您所述,对于单点(即您的GeoJSON图层组包含单点功能),此 衬料 maxZoom 因为实际上没有适合的区域。

    getBounds 返回“空”区域: bounds.getSouthWest().equals(bounds.getNorthEast())

    在这种情况下,你可以使用 mymap.panTo(bounds.getCenter())

    var bounds = myLayer.getBounds();
    
    if (bounds.getSouthWest().equals(bounds.getNorthEast())) {
      // Only a single point feature to view.
      mymap.panTo(bounds.getCenter());
    } else {
      // Some area to fit bounds to.
      mymap.fitBounds(bounds);
    }
    

    https://jsfiddle.net/3v7hd2vx/350/