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?
Best Answer
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 thegeoJsonBounds
function. You need to either pass thegeoJson
layer to the function or define it in a scope this function has access to. Can you post the full code?