I have the following code with an object called Map defined in a namespace RHCA. Map's prototype is assigned an object containing the following methods.
RHCA.Map = function(centerCoords){
this.geojson = null;
this.map = null;
this.initZoom = 0;
this.mapColors = [];
this.mapCenter = centerCoords.slice(0);
this.geoJsonProperty = null;
this.dataFieldsMap;
};
RHCA.Map.prototype = {
setColorRange :function(population){
console.log(this);
var color;
...
return color
},
setStyle : function(feature){
console.log(this);
var population = feature.properties.pop10;
var countyColor = this.setColorRange(population);
return {
fillColor: countyColor,
weight: 0.5,
opacity: 1,
color: '#4e4e4e',
fillOpacity: 0.9,
fillRule: 'evenodd',
className:'svg-custom'
};
},
render : function (){
console.log(this);
var self = this;
var southWest = L.latLng(20.0, -114.0);
var northEast = L.latLng(40.0,-88.0);
var boundRect = L.latLngBounds(southWest, northEast);
self.initZoom = self.setZoomLevel();
self.map = L.map('map',{
center: self.mapCenter,
zoom: self.initZoom,
maxZoom: self.initZoom+2,
minZoom: self.initZoom-0.25,
zoomControl:true,
reUseTiles:true,
unloadInvisibleTiles:true
});
self.geojson = L.geoJson(countyBoundaries,{
style: self.setStyle,
onEachFeature: self.onEachFeature
});
self.geojson.addTo(self.map);
}
};
Then i create the Map object like this.
var map = new RHCA.Map([-31,99]);
I then call the render() function on the map object which will then call setStyle(). setStyle() then calls setColor().
I got the following error from the browser when i called setColorRange() in setStyle() function.
Uncaught TypeError: this.setColorRange is not a function.
When i print 'this' using console.log() in render() it points to the RHCA.Map object,
but when i print 'this' in the setStyle() function it points to the Window Object. I need 'this' to point to the RHCA.Map object instead.
All help is appreciated. Thanks you.
What I have tried:
I tried changing this.setColorRange in the setStyle() function to RHCA.Map.setColorRange but it produces the same error as before.