1

这段代码使用Google Geomap API绘制地图

<script type="text/javascript">
    google.setOnLoadCallback(drawMap);
    function drawMap() {
        var data = new google.visualization.DataTable();
        data.addColumn('string','iso'); //e.g.: mx
        data.addColumn('number','population'); //e.g.: 114
        data.addRows([["mx",114],["ec",14],["ve",28]]);
        var options = {
            region: '005',
            width: '511px',
            height: '280px',
            showLegend: true
        };
        var geomap = new google.visualization.GeoMap(document.getElementById('map'));
        geomap.draw(data,options);
    };
</script>

默认选择的区域是 005(南美洲)。这些是其他可用区域:

  • 013 - (中美洲)
  • 150 - (欧洲)

如何动态更改已绘制地图的区域?像这样的东西:

<a href="javascript:void(null);" onclick="changeRegion(150);">Europe</a>

我知道必须重新绘制地图,但我有点卡在区域属性修改步骤中。任何想法都会有所帮助。谢谢!

4

1 回答 1

1

If you already have the data, I give you a snippet I use in my own app. Easier to understabd rather than long explanations I guess. Hope it helps. In my case, I wanted to display numbers by country in the same region.

geomap = new google.visualization.GeoMap(document.getElementById('geo_chart'));


        google.visualization.events.addListener( geomap, 'regionClick', 
            function(e) 
            { 
                var dataLocal = new google.visualization.DataTable();
                var grouped_cities;

                if (e['region'] == "0")
                    return;

                options['colors'] = [0xaacdf8, 0x164177]; //orange colors
                options['dataMode'] = 'markers';
                options['region'] = e['region'];
                options['showZoomOut'] = true;

                if ( previous_region !=  e['region'] )
                {
                    var indexes  = dataFull.getFilteredRows([{column: 0, value: e['region']}]);

                    dataLocal.addRows(indexes.length);
                    dataLocal.addColumn('string', 'City');
                    dataLocal.addColumn('number', 'Views');

                    for ( i=0; i<indexes.length; i++ )
                    {
                        dataLocal.setValue(i, 0, dataFull.getValue(indexes[i], 2));
                        dataLocal.setValue(i, 1, dataFull.getValue(indexes[i], 3));
                    }


                    grouped_cities = google.visualization.data.group( dataLocal, [0],[{'column': 1, 'aggregation': google.visualization.data.sum, 'type': 'number'}]);
                    previous_region =  e['region'];
                }

                geomap.draw(grouped_cities, options);
                delete grouped_cities;
                delete dataLocal;

            }
        ); 
于 2012-12-17T21:29:20.390 回答