I am working on heatmap and situation is i have to change the color of heat map marker on the basis of some constraints and the color should change according to those basic constraints values. My approach is this:
function HeatMapCreate(heatMapData, GetNumberFromString)
// heatMapData contains the object of LatLng and get number is the constraint
// which contains a number on the basis of which we set color
// (green if 48 and red if 36 else yellow)
{
var gradient;
var heatmap = new google.maps.visualization.HeatmapLayer(
{
data: heatMapData,
});
heatmap.setOptions({ radius: heatmap.get('20') });
if (GetNumberFromString == 36)
{
gradient = [For red color]
}
else if(GetNumberFromString == 48)
{
gradient = [For green color]
}
else
{
gradient = [For yellow color]
}
heatmap.set('gradient', heatmap.get('gradient') ? null : gradient);
heatmap.setMap(map);
}
How to achieve this ? I am bit surprised there is not a single answer (even comment) to this answer, that makes me feel either question is too bad or it is impossible to have heatmap markers of different color ? Could some one please help me ?
Your penultimate line of code looks like you've just copied it from Google's example which demonstrates toggling between two gradients.
It's not the best example to be honest; they're really either setting the gradient to the one they've specified, or setting it to null, i.e. defaulting the gradient back to Google's default colour scheme.
Instead what you want to do is have all your own gradients defined already, and then toggle between them in response to the
GetNumberFromString
value. Here's an untested example. I've gone with a 'blue' colour for the last one, just because it was easier! You probably want to come up with your own set of colours; I expect there's online tools that can produce these for you quite quickly, e.g. http://angrytools.com/gradient/See also: https://developers.google.com/maps/documentation/javascript/heatmaplayer