Leaflet map doesn't always load centered the same WMS Tile Layer


#1

How to reproduce

  • Leaflet version I’m using: 1.0.1
  • Browser (with version) I’m using: Google Chrome (53.0.2785.143)
  • OS/Platform (with version) I’m using: Windows 10
  • Working with: Ionic Framework

Issue description

I’m developing an app using the Ionic framework with AngularJS and the LeafletJS library.

When I load the map for the first time it displays correctly centered.

But, when I change to another view of the app and return to the map view, the WMS Tile Layer displays displaced from the center, only a little bit of it on the top left corner of the map can be seen.

Here it is the snippet of code that creates the map and loads the WMS Tile Layer:

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

Why is the map loading centered only the first time, but displaced after?

I’ve added some console logs to the code in order to debug data passed as parameters to the map’s creation and map’s data after its creation. This way I can compare if something has changed in both situations described earlier. This is the result:

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}}

As can be seen, data passed as center point and max bounds to the map creation is the same in both cases, but once the map has been created, center of the map coordinates and bounds differ a little bit from the first situation to the second one.

I don’t quite understand why it changes.