【问题标题】:Create latLngBounds based on zoom level and a center point根据缩放级别和中心点创建 latLngBounds
【发布时间】:2015-09-26 15:55:34
【问题描述】:

我有一个要素/多边形列表,我需要根据相对于要素的地图边界框运行操作,而无需移动地图。

为了进一步解释我的问题,我可以举一个例子说明如果移动地图不是问题,我该怎么做:

features.forEach(function (feature) {
    var bbox,
        ne,
        sw,
        fBounds,
        zoom,
        mBounds;

    bbox = feature.geometry.bbox;
    sw = L.latLng(bbox[1], bbox[0]);
    ne = L.latLng(bbox[3], bbox[2]);
    fBounds = L.latLngBounds(sw, ne);
    map.fitBounds(bounds);
    mBounds = map.getBounds();
    zoom = map.getZoom();
    //Execute operation based on mBounds and zoom
}    

我已经测试了很多,这是我所拥有的最接近工作代码 sn-p 的东西:

        var self = this,
            bbox,
            sw,
            ne,
            bounds,
            zoom,
            swPoint,
            nePoint,
            center,
            factor,
            dw,
            dh,
            cpx;

        bbox = feature.geometry.bbox;
        sw = L.latLng(bbox[1], bbox[0]);
        ne = L.latLng(bbox[3], bbox[2]);
        bounds = L.latLngBounds(sw, ne);
        zoom = self.map.getBoundsZoom(bounds, false); //maxZoom?
        swPoint = self.map.project(bounds.getSouthWest(), zoom),
        nePoint = self.map.project(bounds.getNorthEast(), zoom),
        center = self.map.unproject(swPoint.add(nePoint).divideBy(2), zoom);

        factor = self.map.options.crs.scale(zoom) / 8388608;
        dw = self.map.getSize().x / 2*factor;
        dh = self.map.getSize().y / 2*factor;
        cpx = self.map.options.crs.latLngToPoint(center, zoom);            

        return {
            ne: self.map.options.crs.pointToLatLng(L.point(cpx.x + dw, cpx.y - dh, false), zoom),
            sw: self.map.options.crs.pointToLatLng(L.point(cpx.x - dw, cpx.y + dh, false), zoom),
            center: center,
            zoom: zoom
        }

        //Execute operation based on returned object, repeat for every feature

这“有效”,但它给出的结果与第一个代码 sn-p 不同(即结果错误)。

【问题讨论】:

    标签: javascript leaflet gis


    【解决方案1】:

    以下 sn-p 对我有用,以防其他人有同样的问题:

    var self = this,
        bbox,
        sw,
        ne,
        bounds,
        zoom,
        center;
    
    bbox = feature.geometry.bbox;
    sw = L.latLng(bbox[1], bbox[0]);
    ne = L.latLng(bbox[3], bbox[2]);
    bounds = L.latLngBounds(sw, ne);
    zoom = self.map.getBoundsZoom(bounds, false); //maxZoom?
    sw = self.map.project(bounds.getSouthWest(), zoom),
    ne = self.map.project(bounds.getNorthEast(), zoom),
    center = self.map.unproject(sw.add(ne).divideBy(2), zoom);
    
    bounds = self.map.getPixelBounds(center, zoom),
    sw = self.map.unproject(b2.getBottomLeft()),
    ne = self.map.unproject(b2.getTopRight());
    
    return new L.LatLngBounds(sw, ne);
    

    【讨论】:

      猜你喜欢
      • 2014-01-01
      • 1970-01-01
      • 2017-06-19
      • 2011-10-26
      • 2013-11-18
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多