1

我目前正在开发一个使用 Google Visualization API 创建热图的 JavaScript 应用程序,但由于某种原因,每当我将我的数据数组添加到热图时,倒数第二个元素总是会引发此错误:

Invalid value at position X [object Object]' when calling method: [nsIDOMEventListener::handleEvent]

谁能向我解释这个错误?我以前从未见过它,我对 SO 和 Google 的搜索没有任何用处。

任何帮助,将不胜感激。

var heatMapOptions =
    {
            data: weightedLocations,
            dissipating: false,
            map: this.googleMap,
            gradient: [
                      'rgba(0, 0, 0, 0)',       //black - transparent
                      'rgba(0, 0, 255, 1)',     //blue
                      'rgba(0, 255, 255, 1)',   //cyan
                      'rgba(0, 238, 0, 1)',     //green
                      'rgba(0, 255, 0, 1)',     //lime-green
                      'rgba(255, 255, 0, 1)',   //yellow
                      'rgba(255, 165, 0, 1)',   //orange
                      'rgba(255, 0, 0, 1)',     //red
                      'rgba(139, 0, 0, 1)'      //dark-red
                    ],
            maxIntensity: 150,
            opacity: 0.6,
            radius: 4 //4 pixels
    };

    //create a Heatmap overlay to sit on top of the google map, passing it the initialization object created above
    var heatmap = new google.maps.visualization.HeatmapLayer({ options: heatMapOptions});

创建对象的位置

function Box(inBox, count)
{
    var box = inBox.toString();
    //strip off any sort keys if they exist
    if(box.indexOf("~") >= 0) {
        box = box.substr(0, box.indexOf('~'));
    }

    //split box into box number and coordinates of four corners
    var splitBox  = box.split("_");
    //console.log(splitBox);

    var boxNumber = splitBox[0];

    this.boxNumber = boxNumber.replace("count|", "");
    this.swlatlon = new google.maps.LatLng( splitBox[1], splitBox[2]);    // a Google LatLng object
    this.selatlon = new google.maps.LatLng( splitBox[3], splitBox[4]);    // a Google LatLng object
    this.nwlatlon = new google.maps.LatLng( splitBox[5], splitBox[6]);    // a Google LatLng object
    this.nelatlon = new google.maps.LatLng( splitBox[7], splitBox[8]);    // a Google LatLng object
    this.points   = [this.swlatlon, this.selatlon, this.nwlatlon, this.nelatlon] //store all points for easy access
    this.count    = count;         // number of ships in this box

    //calculate center of this box
    this.bounds = new google.maps.LatLngBounds(this.swlatlon, this.nelatlon);
    this.center = this.bounds.getCenter();
    this.weightedLocation = { location : this.center, weight : this.count }; // <-- object created here
}

将对象推入数组的位置

function parseJsonForModelData(json, datatype) {
    for(index in json) {
            obj = json[index];

            if (index == "Model Data") 
            {
                $j.each(obj, function(k, v) 
                {
                    if (datatype == 'heat') 
                    {
                        box = new Box(k, v);

                        // Check boxNumber.  If null, skip messes up world map!
                        if (box.boxNumber == 'null') 
                        {
                            badDataCnt++;
                        }
                        else 
                        {
                            modelData.push(box.weightedLocation); <<- pushed here
                        }
                    }
                });
            }
        }
return modelData;
}

加权位置

327: {"location":{"$a":-35.5,"ab":172},"weight":"11"} object music.js:730
328: {"location":{"$a":-36.5,"ab":151},"weight":"1"} object music.js:730
329: {"location":{"$a":-37.5,"ab":151},"weight":"1"} object music.js:730
330: {"location":{"$a":-37.5,"ab":168},"weight":"3"} object music.js:730
331: {"location":{"$a":-37.5,"ab":169},"weight":"6"} object music.js:730
332: {"location":{"$a":-39.5,"ab":161},"weight":"4"} object music.js:730
333: {"location":{"$a":-39.5,"ab":162},"weight":"5"} object music.js:730
334: {"location":{"$a":-39.5,"ab":165},"weight":"6"} object music.js:730 
4

1 回答 1

2

我注意到您发布的 WeightedLocations 有重量字符串。在使用硬编码数据进行测试时,我确实遇到了问题 when weightis a string,所以尝试在 parseInt/float 中使用function Box,希望这能让事情正常进行。

于 2012-07-10T18:25:38.580 回答