简体   繁体   English

Google Maps API-未定义热图(地图显示但无法更改)

[英]Google Maps API - heatmap not defined (map displaying but cannot alter)

On 2nd version of a heatmapping system I am making using google maps api. 在第二版的热图系统中,我正在使用Google Maps API。

Initial version works fine but uses PHP and MySQL to write the points into the map, this displays fine and can then alter map (eg radius / opacity) using heatmap.set the only problem is you cant change heatmap displayed without reloading (points are effectively hard coded into script by PHP). 初始版本工作正常,但使用PHP和MySQL将点写入地图,这可以正常显示,然后可以使用heatmap更改地图(例如,半径/不透明度)。唯一的问题是您无法更改显示的热量图而无需重新加载(点有效由PHP硬编码为脚本)。

So I have altered original code to instead call a php script (using a serialized version of form) which returns query in JSON format, this works fine in as far as get a blank map to begin with, can choose some settings and it will load heatmap fine, can change these and it will do a new heatmap (which it layers on top of old one) so have gone through how to remove previous heatmap and hit a brick wall... 因此,我更改了原始代码,改为调用php脚本(使用表单的序列化版本),该脚本以JSON格式返回查询,就可以开始使用空白地图而言,它可以正常工作,可以选择一些设置,它将加载好的热图,可以更改这些,它将做一个新的热图(将其叠加在旧的热图上),因此介绍了如何删除以前的热图并撞到砖墙...

If I try any function calling heatmap. 如果我尝试任何函数调用heatmap。 I get "heatmap not defined" this includes all the previous functions that worked fine in old map. 我得到“未定义热图”,其中包括所有在旧地图中都能正常工作的先前功能。

Full code is quite large so will hopefully cover everything in these snippets... 完整的代码很大,因此希望可以覆盖这些摘要中的所有内容。

Initial map load... 初始地图加载中...

  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 13,
      center: {lat: 51.514898, lng: -0.144432},
      mapTypeId: 'roadmap'
    });
  }

Make a heatmap... 制作热图...

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
    var heatmapData = [];
    myObj = JSON.parse(this.responseText);
    for (var i = 0; i < myObj.points.length; i++) {
    var latLng = new google.maps.LatLng(myObj.points[i].lat,myObj.points[i].lng);
    var pushData = {
            location: latLng,
            weight: myObj.points[i].weight
    }
    heatmapData.push(pushData);
    }
var heatmap = new google.maps.visualization.HeatmapLayer({
  data: heatmapData,
  maxIntensity: 10,
  radius:30,
  opacity: 0.4,
  map: map
})
}
};

Which is called by form submit... 表单提交称为...

$( "form" ).on( "submit", function( event ) {
  event.preventDefault();
  url = "query.php?" + $( this ).serialize();
  xmlhttp.open("GET", url, true);
  xmlhttp.send();
});

and an example function which don't work (value set by a slider with an onchange event)... 和一个不起作用的示例函数(由带有onchange事件的滑块设置的值)...

  function changeOp(val) {
    heatmap.set('opacity', val);
  }

The heatmap is currently local to the xmlhttp.onreadystatechange function, you can't access it outside of there. 该热图当前位于xmlhttp.onreadystatechange函数的本地,您无法在此之外访问它。 You need to declare it outside of that function (the global scope works, like the map variable), then you can access it to modify it later (or in the next call to the AJAX request). 您需要在该函数之外声明它(全局作用域就像map变量一样工作),然后可以访问它以稍后对其进行修改(或在下一次对AJAX请求的调用中)。

var heatmap;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var heatmapData = [];
    myObj = JSON.parse(this.responseText);
    for (var i = 0; i < myObj.points.length; i++) {
      var latLng = new google.maps.LatLng(myObj.points[i].lat, myObj.points[i].lng);
      var pushData = {
        location: latLng,
        weight: myObj.points[i].weight
      }
      heatmapData.push(pushData);
    }
    heatmap = new google.maps.visualization.HeatmapLayer({
      data: heatmapData,
      maxIntensity: 10,
      radius: 30,
      opacity: 0.4,
      map: map
    })
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM