javascript - 谷歌热图根据强度改变颜色

标签 javascript google-maps google-maps-api-3 maps heatmap

我使用 google maps js api 制作了一个热图。所有位置都已加权,并且希望颜色代表权重,而不是我得到一个红点,该红点逐渐变为黄色然后变为绿色。这不仅仅是一个测试,我将从数据库中填充邮政编码和权重

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=visualization"></script>

.

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", 6119);
  codeAddress("10002", 5180);
  codeAddress("10003", 4110);
  codeAddress("10004", 899);
  codeAddress("10005", 520);
  codeAddress("10006", 599);

   function codeAddress(zip, noAccidents) {
    //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;
        console.log(hotSpot + " " + noAccidents);

        var heatMapZip = [
        {location: hotSpot, weight: noAccidents}

        ];

       var color =[
            "#ff0000",
            "#00ff00"
        ];

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

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


}

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

MAP

最佳答案

您应该使用函数 heatmap.set('gradient', gradient); 来设置热图的颜色。颜色可以通过意外的#以及数据集中#的最大值和最小值来计算。

我为此创建了一个 fiddle ,希望它能有所帮助。

http://jsfiddle.net/hzy0y6es/

关于javascript - 谷歌热图根据强度改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31342602/

相关文章:

google-maps-api-3 - 自定义绘图控件 [Google Maps v3 绘图库]

javascript - 如何使用我的数据库中的纬度和经度值在谷歌地图上显示?

javascript - 如何制作pjax :popstate work like a simple PJAX call

android - Google map v2 授权失败。不同的 SHA1

java - 使用 Firestore 数据库在 map 上创建多个标记

javascript - 检测圆是否在 map 内

javascript - 将HTML类/ID添加到Google Maps Marker(API V3)

javascript - 关闭 InAppBrowser 后,我无法移至下一页

javascript - 刷新包含属性的元素

javascript - 如何获取 .js 文件本身的 uri