热图谷歌地图错误与阵列

时间:2017-03-11 22:15:00

标签: javascript google-maps

我是google maps y JS的新手,我有这个问题。

这是我的代码:

var map, heatmap, data, position;

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 13,
    center: {lat: 37.775, lng: -122.434},
    mapTypeId: google.maps.MapTypeId.SATELLITE
  });

  heatmap = new google.maps.visualization.HeatmapLayer({
    data: makeRequest('jsonmapasenal.php', function(data) {

         data = JSON.parse(data.responseText);
         data2 = [], range = data.length;
        for (i = 0; i < data.length; i++) {


             data2[i]= new google.maps.LatLng(parseFloat(data[i].lat), parseFloat(data[i].lon));  ---------Here is the problem
        }


    console.log(data2);
    return data2;
    }),
    map: map
  });
}

function toggleHeatmap() {
  heatmap.setMap(heatmap.getMap() ? null : map);
}

function changeGradient() {
  var gradient = [
    'rgba(0, 255, 255, 0)',
    'rgba(0, 255, 255, 1)',
    'rgba(0, 191, 255, 1)',
    'rgba(0, 127, 255, 1)',
    'rgba(0, 63, 255, 1)',
    'rgba(0, 0, 255, 1)',
    'rgba(0, 0, 223, 1)',
    'rgba(0, 0, 191, 1)',
    'rgba(0, 0, 159, 1)',
    'rgba(0, 0, 127, 1)',
    'rgba(63, 0, 91, 1)',
    'rgba(127, 0, 63, 1)',
    'rgba(191, 0, 31, 1)',
    'rgba(255, 0, 0, 1)'
  ]
  heatmap.set('gradient', heatmap.get('gradient') ? null : gradient);
}

function changeRadius() {
  heatmap.set('radius', heatmap.get('radius') ? null : 20);
}

function changeOpacity() {
  heatmap.set('opacity', heatmap.get('opacity') ? null : 0.2);
}

 function makeRequest(url, callback) {
    var request;

    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari

    } else {
        request = new ActiveXObject("Microsoft.XMLHTTP"); // IE6, IE5

    }
    request.onreadystatechange = function() {
        if (request.readyState == 4 && request.status == 200) {
            callback(request);

        }
    }
   request.open("GET", url, true);

    request.send();


}

在控制台中,结果为:

Uncaught Gc {message:“not a Array or MVCArray”,name:“InvalidValueError”,stack:“Error↵at new Gc(https://maps.googleapis.com/m ... [_。K,_. K,_。K, _.K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_.K,_。 K,_.K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_.K,_. K,_. K,_.K, _.K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_.K,_。 K,_.K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_.K,_. K,_. K,_.K, _.K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_.K,_。 K,_.K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_.K,_. K,_. K,_.K, _.K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_. K,_.K] 0:_.K1: _.K2:_. K3:_. K4:_. K5:_. K6:_. K7:_. K8:_. K9:_. K10:_. K11:_. K12:_.K13:_。 K14:_. K15:_. K16:_.K17:_.K18:_.K19:_.K20:_. K21:_.K22:_.K23:_.K24:_.K25:_.K26: _.K27:_.K28:_.K29:_.K30:_. K31:_. K32:_. K33:_. K34:_. K35:_. K36:_.K37:_.K38:_。 K39:_. K40:_. K41:_. K42:_. K43:_. K44:_.K45:_.K46:_.K47:_.K48:_.K49:_.K50:_. K51: _.K52:_. K53:_. K54:_. K55:_. K56:_. K57:_. K58:_. K59:_. K60:_. K61:_. K62:_. K63: _.K64:_. K65:_. K66:_. K67:_. K68:_. K69:_. K70:_. K71:_. K72:_。K73:_. K74:_. K75:_。 K76:_.K77:_.K78:_.K79:_. K80:_.K81:_.K82:_.K83:_.K84:_.K85:_.K86:_.K87:_.K88: _.Klength:89__proto__:数组[0]

如果我包含'喜欢这个:

data2 [i] ='new google.maps.LatLng(parseFloat('+ data [i] .lat +'),parseFloat('+ data [i] .lon +'))';

工作数组填充,但控制台仍然抛出错误:不是数组或MVCArray

请帮助!

1 个答案:

答案 0 :(得分:1)

我不明白问题是什么,但是当我开始测试热图时我也遇到了一些问题。在这里,我有一个简单的热图呼叫,我几个星期前做过。看看我在HeatMap上做了些什么。 P.S:下次尝试更具体地解决你的问题......

&#13;
&#13;
function myFunction {
  mapOptions = {
    center: new google.maps.LatLng(some_lat, some_long),
    zoom: 9,
    mapTypeId: google.maps.MapTypeId.HYBRID
  };
  map = new google.maps.Map(document.getElementById('googleMap'), mapOptions);

  var heatmap = new google.maps.visualization.HeatmapLayer({
    data: arrHeatmap // this array contains an x number of google.Maps.LatLng 
  });
  heatmap.setMap(map);
  console.log("Heatmap loaded successfully!");
 }
&#13;
&#13;
&#13;