如何在地理地图中提供美国州代码而非国家代码?

时间:2019-01-31 10:13:50

标签: javascript geomap

我试图根据某些价值来识别美国并为其着色。但这仅需要国家代码。我尝试了以下代码-

 var geomap_chartdata = google.visualization.arrayToDataTable([
            ['States', 'Popularity'],
            ['ID', 200],
            ['IN', 300],
            ['KY', 400],
            ['LA', 500],
            ['MA', 600],
            ['MT', 600],
            ['OH', 600],
            ['NY', 600],
            ['WA', 700]
        ]);

        var options = {};
        options['displayMode'] = 'regions';
 var chart = new google.visualization.GeoChart(document.getElementById('revenue_inbound_map'));
        chart.draw(geomap_chartdata, options);

但这不起作用。无需识别州代码地理地图,而是将其识别为国家代码。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:3)

在选项中,您需要提及所要查找的内容以及分辨率。因此,将区域设为美国,将显示模式设为区域。试试这个:

  google.charts.load('current', {
    'packages':['geochart'],
    // Note: you will need to get a mapsApiKey for your project.
    // See: https://developers.google.com/chart/interactive/docs/basic_load_libs#load-settings
    'mapsApiKey': 'AIzaSyD-9tSrke72PouQMnMX-a7eZSW0jkFMBWY'
  });
  google.charts.setOnLoadCallback(drawRegionsMap);

  function drawRegionsMap() {
    var data = google.visualization.arrayToDataTable([
      ['States', 'Popularity'],
        ['ID', 200],
        ['IN', 300],
        ['KY', 400],
        ['LA', 500],
        ['MA', 600],
        ['MT', 600],
        ['OH', 600],
        ['NY', 600],
        ['WA', 700]
    ]);



     var opts = {
      region: 'US',
      displayMode: 'regions',
      resolution: 'provinces',
      width: 640, 
      height: 480
    };

    var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));

    chart.draw(data, opts);
  }