Google Maps API v 3:热图创建问题

时间:2012-07-02 15:18:29

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

我正在尝试使用google.maps.visualization库。我已经将库包含在我的代码中以及所有这些中,并且大部分内容似乎正在工作,但是当我到达创建热图时,我收到有关'位置96677处的无效值的错误:[object Object]' 。我不确定这个问题是什么,但错误报告包含一行Google API代码:

var c;
M(b,function(b,e){
    try{
        a(b)||(c="Invalid value at position "+(e+(": "+b)))
    }catch(f){
        c="Error in element at position "+(e+(": ("+(f[Pb]+")")))}});c&&aa(ja(c));return j
    }
}

我仔细查看了API代码,但在那里找到任何有用的东西都很困难。我用来创建热图图层的代码如下:

$.ajax({
    url: "../Yield/getYieldData.php",
    success: function(text) {
        var data;
//      var yieldPoints = new google.maps.MVCArray();
        var yieldPoints = [];

        try{
            data = $.parseJSON(text);
        } catch (e) {
            alert("ERROR: " + e);
        }

        for(i=0; i < data.points.length; i++) {
//          yieldPoints.push({ location: new google.maps.LatLng(data.points[i].lat, data.points[i].lon), weight: data.points[i].yield });
            yieldPoints[i] = { location: new google.maps.LatLng(data.points[i].lat, data.points[i].lon), weight: data.points[i].yield };
        }
        var heatMap = new google.maps.visualization.HeatmapLayer({ data: yieldPoints });
        heatMap.setMap(map);
    }
});

我不确定这里会出现什么问题,但我使用Google API文档作为模板,如https://developers.google.com/maps/documentation/javascript/layers#JSHeatMaps所示(大约三分之一的页面下方标题'添加加权数据点')。正如您所看到的,我还尝试将该数组创建为MVCArray,如该文档中所述,但这并未改变任何内容。如果它是相关的,这里是从'getYieldData.php'调用接收的JSON数据的简短示例:

{"points":[{"lat":"38.1513366000","lon":"-97.4341659000","yield":"0"},{"lat":"38.1513748000","lon":"-97.4341125000","yield":"0"},{"lat":"38.1513938000","lon":"-97.4341125000","yield":"0"},{"lat":"38.1493263000","lon":"-97.4339447000","yield":"0"},{"lat":"38.1493339000","lon":"-97.4339447000","yield":"0"},{"lat":"38.1493377000","lon":"-97.4339447000","yield":"0"},{"lat":"38.1483650000","lon":"-97.4358291000","yield":"0"},{"lat":"38.1484031000","lon":"-97.4358062000","yield":"0"},

希望这足以让人知道问题是什么 - 任何帮助都会非常感激。谢谢。

1 个答案:

答案 0 :(得分:5)

weight应该是Number,而不是String。所以你有两个选择:

  • 避免使用JSON中的引号:

    {"lat":"38.1513366000","lon":"-97.4341659000","yield":0}
                  //no quote wraps    'yield' ------------^
    
  • yield投射到Number

    yieldPoints[i] = { 
               location: new google.maps.LatLng(data.points[i].lat, data.points[i].lon),
               weight: +data.points[i].yield };
    };                 ^----- plus('+') is the fastest way for converting string to number