javascript - 传单 map 并不总是加载居中的相同 WMS 平铺层

标签 javascript maps leaflet coordinates wms

我正在使用 Ionic 框架、AngularJS 和 LeafletJS 库开发一个应用程序。

当我第一次加载 map 时,它显示正确居中。

但是,当我切换到应用程序的另一个 View 并返回到 map View 时,WMS 平铺层显示偏离中心,只能看到 map 左上角的一小部分。

这是创建 map 并加载 WMS 平铺图层的代码片段:

function crearPlano($scope, $http, infoService, sharedProperties, poisService, createModal) {

    var building = localStorage.building,
        floor = localStorage.planta,
        floor_id = building + floor,
        building_id = floor_id.replace(/\./g,"_").toLowerCase();

    var parameters = 
    [
        'proyecto/ows',
        'service=WFS',
        'version=1.0.0',
        'request=GetFeature',
        'typeName=proyecto:'+building_id,
        'srsName=epsg:4326',
        'outputFormat=application/json'
    ]
    var url = APP_CONSTANTS.URI_Geoserver_1 + 'proyecto/ows?' + parameters.join('&');

    $.ajax({
        url : url,
        type: 'GET',
        dataType : 'json',
        crossDomain: true,
        headers: { 'Access-Control-Allow-Origin': '*' },
        success: function(data) {
            handleJson(data, sharedProperties, poisService, createModal, function(map){
                sharedProperties.setMap(map);
            });
        }
    });

    function handleJson(data, floor_id, sharedProperties, poisService, createModal, callback) {
        console.log("handleJson", data, floor_id);
        var map = sharedProperties.getMap(),
            coordinates = data.features[0].geometry.coordinates[0][0][0],
            floorCoordinates = new L.latLng(coordinates[1], coordinates[0]);

        //Remove previous plan layers
        if(!(typeof map == 'undefined')) { map.remove(); }

        var imageLayer = new L.tileLayer.wms(APP_CONSTANTS.URI_Geoserver_2 + "sigeuz/wms", 
        {
            layers: 'sigeuz:vista_plantas',
            maxZoom: 25,
            zIndex: 5,
            viewparams : 'PLANTA:'+floor_id,
            format: 'image/png', transparent: true, attribution: floor_id
        });

        console.log("Before create map  -->  Center", JSON.stringify(floorCoordinates));
        console.log("Before create map  -->  MaxBounds", JSON.stringify(L.geoJson(data).getBounds()));

        map= new L.map('plan'
            ,{
                crs: L.CRS.EPSG3857,
                layers: [imageLayer]
            }
        ).setView(floorCoordinates, 20);

        console.log("After create map  -->  Center", JSON.stringify(plano.getCenter()));
        console.log("After create map  -->  Bounds", JSON.stringify(plano.getBounds()));

        callback(map);
    }
}

为什么 map 第一次加载时居中,之后就移位了?

我在代码中添加了一些控制台日志,以便调试作为参数传递给 map 创建的数据以及创建后的 map 数据。这样我就可以比较前面描述的两种情况是否发生了变化。这是结果:

Before create map  -->  Center {"lat":41.64063807836347,"lng":-0.90146666131869}
Before create map  -->  MaxBounds {"_southWest":{"lat":41.64061302810611,"lng":-0.9015017606364195},"_northEast":{"lat":41.64079735418267,"lng":-0.9012732012812255}}

After create map  -->  Center {"lat":41.6406381751715,"lng":-0.9014656782889374}
After create map  -->  Bounds {"_southWest":{"lat":41.64032848115259,"lng":-0.9017432869219788},"_northEast":{"lat":41.640947867702096,"lng":-0.9011880696558963}}


Before create map  -->  Center {"lat":41.64063807836347,"lng":-0.90146666131869}
Before create map  -->  MaxBounds {"_southWest":{"lat":41.64061302810611,"lng":-0.9015017606364195},"_northEast":{"lat":41.64079735418267,"lng":-0.9012732012812255}}

After create map  -->  Center {"lat":41.64063807836347,"lng":-0.90146666131869}
After create map  -->  Bounds {"_southWest":{"lat":41.640637639043334,"lng":-0.9014663100242616},"_northEast":{"lat":41.640637639043334,"lng":-0.9014663100242616}}

可以看出,两种情况下作为中心点和最大边界传递到 map 创建的数据是相同的,但是创建 map 后, map 中心坐标和边界与第一种情况略有不同到第二个。

我不太明白为什么会改变。

最佳答案

运行invalidateSize()每次 map 容器可能改变大小时。这包括通过 jquery、ionic 等隐藏/显示它。

关于javascript - 传单 map 并不总是加载居中的相同 WMS 平铺层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40120142/

相关文章:

javascript - 使用 javascript 按下更改时执行某些操作

javascript - 每 18 小时循环倒数计时器,并具有特定的开始日期

javascript - 使用 Firefox 插件保存网页,使用文件 -> 另存为弹出窗口

cocoa - Cocoa 的开源 map 框架

javascript - 使用 Drupal Leaflet 模块更改 Leaflet map 缩放控制样式

javascript - Highcharts C# 到 JSON - 构建系列数据

R tmap动态修改图例以防止图例值重叠

ios - MKLocalSearch 返回区域外的结果

r - 根据输入更改传单 map ,无需重绘

javascript - 如何在传单中的标记下方添加文本?