如何在预先确定的约束值上自定义热图颜色?

时间:2015-06-19 08:45:44

标签: javascript google-maps-api-3 google-maps-markers google-maps-api-2 heatmap

我正在研究热图,情况是我必须根据一些约束改变热图标记的颜色,颜色应根据这些基本约束值而改变。 我的方法是:

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

如何实现这一目标? 我有点惊讶这个答案没有一个答案(甚至是评论),这让我觉得问题太糟糕或者不可能有不同颜色的热图标记?可能有人请帮帮我?

2 个答案:

答案 0 :(得分:1)

您的倒数第二行代码看起来就像是从Google's example复制了它,它展示了在两个渐变之间切换。

heatmap.set('gradient', heatmap.get('gradient') ? null : gradient);

说实话,这不是最好的例子;他们真的要么将渐变设置为他们指定的渐变,要么将其设置为null,即将渐变默认为Google的默认颜色方案。

相反,您想要做的是已经定义了所有自己的渐变,然后在它们之间切换以响应GetNumberFromString值。这是一个未经考验的例子。我已经选择了蓝色'最后一个的颜色,只是因为它更容易!你可能想要提出自己的一套颜色;我希望有那些可以很快为您制作这些工具的在线工具,例如: http://angrytools.com/gradient/

function HeatMapCreate(heatMapData, GetNumberFromString) {
    var gradients = {
        red: [
            'rgba(255, 0, 0, 0)',
            'rgba(255, 0, 0, 1)'
        ],
        green: [
            'rgba(0, 255, 0, 0)',
            'rgba(0, 255, 0, 1)'
        ],
        blue: [
            'rgba(0, 0, 255, 0)',
            'rgba(0, 0, 255, 1)'
        ]
    };
    var heatmap = new google.maps.visualization.HeatmapLayer({
        data: heatMapData,
        radius: 20,
        map: map
    });

    switch (GetNumberFromString) {
        case 36:
            heatmap.set('gradient', gradients['red']);
            break;
        case 48:
            heatmap.set('gradient', gradients['green']);
            break;
        default:
            heatmap.set('gradient', gradients['blue']);
            break;

    }
}

另见: https://developers.google.com/maps/documentation/javascript/heatmaplayer

答案 1 :(得分:0)

您无法使用单个热图实现不同的颜色。您必须根据GetNumberFromString函数返回值为要显示的每种颜色使用单独的热图图层。

查看以下代码。

function initialize() {
  geocoder = new google.maps.Geocoder();
  var mapProp = {
    center: new google.maps.LatLng(40.785091, -73.968285),
    zoom: 11,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
  codeAddress("10001", 'rgba(1, 1, 0, 0)', 'rgba(1, 1, 0, 1)');
  codeAddress("10002", 'rgba(255, 255, 0, 0)', 'rgba(255, 255, 0, 1)');
  codeAddress("10003", 'rgba(255, 0, 0, 0)', 'rgba(255, 0, 0, 1)');
  codeAddress("10004", 'rgba(0, 255, 0, 0)', 'rgba(0, 255, 0, 1)');
  codeAddress("10005", 'rgba(153, 0, 153, 0)', 'rgba(153, 0, 153, 1)');
  codeAddress("10006", 'rgba(1, 1, 0, 0)', 'rgba(1, 1, 0, 1)');

  function codeAddress(zip, rgbA1, rgbA2) {
    //var address = document.getElementById("address").value;
    geocoder.geocode({
      'address': zip
    }, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);

        var hotSpot = results[0].geometry.location;

        var heatMapZip = [{
            location: hotSpot
          }

        ];

        var heatmap = new google.maps.visualization.HeatmapLayer({
          data: heatMapZip,
          radius: 20,
          dissapating: false
        });

        var gradient = [rgbA1, rgbA2];
        heatmap.set('gradient', gradient);
        heatmap.setMap(map);

      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }


}

google.maps.event.addDomListener(window, 'load', initialize);