简体   繁体   English

将地区网址添加到Geochart

[英]Add Region URL to Geochart

I am creating a map using Google Geochart and need a listener so that when the user clicks on a region it loads a given URL. 我正在使用Google Geochart创建地图,并且需要一个侦听器,以便用户单击区域时可以加载给定的URL。

My code is: 我的代码是:

google.load('visualization', '1.1', {packages: ['geochart'], callback: drawVisualization});

  function drawVisualization() {
   var data = google.visualization.arrayToDataTable([
    ['Country', 'Value', {role: 'tooltip', p:{html:true}}],
    ['US', 20, '<a href="http://www.ipfa.org/council/branches/39/ipfa-americas/">Test</a>'],
    ['Canada', 20, 'http://www.ipfa.org/council/branches/106/ipfa-canada/'],
    ['GB', 20, 'http://www.ipfa.org/council/branches/52/ipfa-uk/'],
   ]);

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

   google.visualization.events.addListener(chart, 'select', function () {
    var selection = chart.getSelection();
    var row = selection[0].row;
    var url = data.getValue(row, 3);
    window.open(url);
   });

    chart.draw(data, {
     width: 800,
     height: 600,
     tooltip: {
      isHtml: true
     }
    }
   );
  }

The URL listener works on another map I use, what am I doing wrong to not work on this one? URL侦听器可在我使用的另一张地图上使用,如果在此地图上无法使用,我在做什么错呢?

There are two issues. 有两个问题。 First, you are using the wrong index to reference your URLs; 首先,您使用错误的索引来引用您的URL; they are in column 2, not column 3 (which doesn't exist): 它们位于第2列,而不是第3列(不存在)中:

var url = data.getValue(row, 3);

Second, one of your URL's (for the US) is an anchor tag, which won't work if passed to the window.open call. 其次,您的网址之一(对于美国)是锚标记,如果传递给window.open调用,该标记将无效。 If you want anchor tags in the tooltips, set the value of the cell to the URL and formatted value of the URL column to the anchor tag: 如果要在工具提示中使用锚标记,则将单元格的值设置为URL,并将URL列的格式化值设置为锚标记:

['US', 20, {v: 'http://www.ipfa.org/council/branches/39/ipfa-americas/', f: '<a href="http://www.ipfa.org/council/branches/39/ipfa-americas/">Test</a>'}]

I would also suggest testing for the length of the selection array, because it is possible for the selection array to be empty if the user clicks a region twice in a row (the second click deselects the region), which would cause this line to throw an error: 我还建议测试选择数组的长度,因为如果用户连续两次单击某个区域(第二次单击取消选择该区域),则选择数组有可能为空,这将导致此行抛出一个错误:

var row = selection[0].row;

I suggest using this instead: 我建议改用这个:

var selection = chart.getSelection();
if (selection.length) {
    var url = data.getValue(selection[0].row, 2);
    window.open(url);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM