2

I have some codes used Leaflet and I want to refactor them below one is working:

jQuery(document).ready(function(){
                    $.post("StatisticsResultServlet",
                    {
                      queryId:queryIdStr,     
                      field:fieldName,
                      groupingLevelValue:groupingLevel,
                      selectedCityName:selectedCity,
                      divideByPopulation:divideByPop,
                      nofLegendGap:numberOfLegendGap,
                      multipliedNormalizationConstant:multipliedConstant
                    },
                    function(data,status){

                        statisticsResultArray = data.resultArray;
                        legendRangeArray = data.legendRanges;

                        var geojsonFeature;
                        if(groupingLevel == 1){
                            geojsonFeature = BOLGE;
                        }else if(groupingLevel ==2){
                            geojsonFeature = IL;
                        }else if(groupingLevel ==3){
                            geojsonFeature = eval(selectedCity);
                        }
                        geojson = L.geoJson(geojsonFeature, {
                        style: style,
                        onEachFeature: onEachFeature
                        });

                        geojson.addTo(map);

                        var southWest  = geojson.getBounds().getSouthWest();
                        var northEast  = geojson.getBounds().getNorthEast();
                        var bounds = new L.LatLngBounds(southWest, northEast);
                        map.fitBounds(bounds);

                        prepareLegend();

                    });

                });

This method is too long and I separated to different classes the business logic. Related my class which I got the error is below:

function PolygonService(){
     this.createPolygon = function(geoJsonFeature){....
     this.geoJsonBounds = function(map){
        var boundsOfGeoJson;

        var northEast  = geoJson.getBounds().getNorthEast();
        var southWest  = geoJson.getBounds().getSouthWest();
        boundsOfGeoJson = new L.latLngBounds(southWest, northEast);

        map.fitBounds(boundsOfGeoJson);

    };

The error I got

TypeError: this._northEast is undefined

How to solve the problem?

2

what this error tells you is that you've lost the reference to the L.geoJSON layer you have named geoJson. It is not in scope in the geoJsonBounds function. You need to either pass the geoJson layer to the function or define it in a scope this function has access to. Can you post the full code?

|improve this answer|||||
1

What is the size of your geojson? I think you need to register 'layeradd' map event, this event will be called only after the geojson [or any other layer] is properly added to the map. In this event, you can get the layer object and its extent, and then call map.fitBounds(bounds) to zoom to the layer.

Here is the basic code:

map.on('layeradd', function (e) {
            var southWest = e.layer.getBounds().getSouthWest();
            var northEast = e.layer.getBounds().getNorthEast();
            var bounds = new L.LatLngBounds(southWest, northEast);
            map.fitBounds(bounds);
        });
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.