What is the process for incorporating links into Google GeoChart maps?

I'm currently working on developing a unique clickable map using Google's GeoChart for a website. My goal is to make each country clickable so that users can be directed to separate web pages when they select different countries on the map. Additionally, I would like the color of the selected country to change in order to indicate its selection. If anyone could assist me with creating the JavaScript code to implement the select event, it would be greatly appreciated. Below is the code snippet I have managed to put together so far:

<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js">
</script>

<script type="text/javascript">
google.charts.load('current', {
  'packages':['geochart'],
  'mapsApiKey': 'AIzaSyD3MfRYHAynUsxWCZ8NDsA3cwvWlTkhT1s'
});
google.charts.setOnLoadCallback(drawRegionsMap);

function drawRegionsMap() {
  var data = google.visualization.arrayToDataTable([
 ['Country'],
    ['Thailand' ],
    ['India'],
    ['Malaysia'],
    ['Sri Lanka'],
    ['Indonesia'],
    ['Vietnam'],
    ['Korea'],
    ['Taiwan'],
    ['China'],  
  ]);

  var options = {
    region: '142', // Asia
    colorAxis: {colors: ['#f5f5f5']},
    datalessRegionColor: '#f5f5f5',
    defaultColor: '#ff8040',
  };

  var chart = new google.visualization.GeoChart(document.getElementById('geochart-colors'));
  chart.draw(data, options);
};
</script>
</head>
<body>
<div id="geochart-colors" style="width: 700px; height: 433px;"> </div>
</body>
</html>

Answer №1

If you take a look at the code snippet provided, it's clear that you need to incorporate a listener using google.visualization.events.addListener. Within this listener, you can extract the name of the selected country and manipulate it as needed. This information can be output using console.log() for further customization.

google.charts.load('current', {
    'packages':['geochart'],
    'mapsApiKey': 'AIzaSyD3MfRYHAynUsxWCZ8NDsA3cwvWlTkhT1s'
  });
  google.charts.setOnLoadCallback(drawRegionsMap);

  function drawRegionsMap() {
    var data = google.visualization.arrayToDataTable([
   ['Country'],
      ['Thailand' ],
      ['India'],
      ['Malaysia'],
      ['Sri Lanka'],
      ['Indonesia'],
      ['Vietnam'],
      ['Korea'],
      ['Taiwan'],
      ['China'],  
    ]);

    var options = {
      region: '142', // Asia
      colorAxis: {colors: ['#f5f5f5']},
      datalessRegionColor: '#f5f5f5',
      defaultColor: '#ff8040',
    };

    var chart = new google.visualization.GeoChart(document.getElementById('geochart-colors'));
    chart.draw(data, options);

  
  google.visualization.events.addListener(chart, 'select', function() {
    var selectedItem = chart.getSelection()[0];
    if (selectedItem) {
      var country = data.getValue(selectedItem.row, 0);
      console.log(country);

      
    }    
  });  };
<html>
  <head>
  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js">
  </script>
 
  </head>
  <body>
  <div id="geochart-colors" style="width: 700px; height: 433px;">  </div>
   </body>
  </html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sending messages to all sockets in socket.io except the one who sent it

I'm currently working on integrating a chat feature into my app using socket.io. The process involves sending an API request to the server each time a user sends a message, which is then stored in the database. Only after this data storage step is com ...

JavaScript tip: How to disregard symbols that affect the length of text in a textbox?

I am working on a task which involves counting only the text, excluding symbols, entered in a textbox. For instance, if a user types email_ex_3/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05374568646c692b666a68">[email ...

Server-side props become inaccessible on the client side due to middleware usage

I'm attempting to implement a redirect on each page based on a specific condition using Next.js middleware. Strange enough, when the matcher in middleware.ts matches a page, all props retrieved from getServerSideProps for that page end up being undef ...

Error Reference: The variable "angular" has not been properly defined in line 1 of the app.js

WelcomePage.js angular.module("travel") .controller('LoginCtrl', function($scope, AuthService, $ionicPopup, $state) { $scope.user = { name: '', password: '' }; $scope.login = function() { AuthService.login ...

Obtaining Input in NodeJS

I am working on a code that captures user input but I am facing an issue. Currently, the code does not wait for the input to be completed. I am looking for a solution where the code will pause until the user has entered their input. It is crucial for me th ...

Undisclosed iFrame technique for uploading files - issue with nested forms

This question seems to be more related to JavaScript/jQuery/DOM rather than Rails, but it's all linked to how I integrated it in Rails. I'm currently working on allowing users to upload multiple photos while creating a new object (auction). The c ...

Issue encountered when installing packages with NPM due to a missing first argument

Encountering this issue when attempting to install packages using npm install. Looks like there is a problem with npm. I am currently running Linux Mint 19.3 Cinnamon. npm ERR! Linux 5.4.0-42-generic npm ERR! argv "/usr/bin/node" "/usr/bin ...

What is the method for assigning a value to a Material UI text field?

Trying to create an autocomplete search feature using EsriGeocode and Material UI. The form includes Street name, City, State, and Zip code fields. Currently facing an issue where the Street name text field displays the entire address instead of just the s ...

Navigating the DOM in Vue.js: A Step-by-Step Guide

While I am trying to utilize Vue.js to access the DOM, the code I have written within the mounted() lifecycle is not producing any results. Interestingly enough, the same code functions perfectly fine when using vanilla JavaScript. I have incorporated the ...

Retrieving image source from JSON data using AngularJS

I am attempting to retrieve an image from an API using AngularJS. The approach I'm taking involves triggering the $http function with a link and attempting to extract the image URL from the JSON response to display it in the HTML. However, despite re ...

Having trouble accessing the Webservice through AJAX

Attempting to call a webservice method using an AJAX method is proving challenging. Despite the effort, accessing the Webservice method through the AJAX Call remains unsuccessful. The expected outcome is for the webservice to return a JSON string in the aj ...

Can a webpage be sent to a particular Chromecast device without using the extension through programming?

My organization has strategically placed multiple Chromecasts across our facility, each dedicated to displaying a different webpage based on its location. Within my database, I maintain a record of the Chromecast names and their corresponding URLs. These d ...

Angular fails to include the values of request headers in its requests

Using Django REST framework for the backend, I am attempting to authenticate requests in Angular by including a token in the request headers. However, Angular does not seem to be sending any header values. Despite trying various methods to add headers to ...

Converting to alphanumeric characters using JavaScript

Is there a way to efficiently encode a random string into an alphanumeric-only string in JavaScript / NodeJS while still being able to decode it back to the original input? Any suggestion on the best approach for this would be greatly appreciated! ...

Apply a class using [ngClass] based on an HTML condition within the local scope

In the past, I have utilized [ngClass] to assign classes based on the Boolean value of a variable in the JavaScript/TypeScript. However, I am now curious if it is achievable to apply [ngClass] based on a local HTML boolean value instead? For example: < ...

Using Ajax.BeginForm with BeforeSend functionality

My MVC website has multiple Ajax.BeginForm elements, and I am looking to handle the beforeSend event of my Ajax calls. While the code below works for manual jquery ajax calls, it does not seem to work with the Ajax.BeginForm helpers: $.ajaxSetup({ &a ...

Angular 7/8 now allows for empty strings in Template Driven Forms

I've encountered a problem with my form validation that allows empty strings. The required attribute works, but it still allows the user to input spaces. I came across a solution online which involves using ng-pattern with the pattern pattern=".*[^ ]. ...

Removing data entry from MySQL database table using PDO

I'm facing an issue with deleting a row from my database. Despite trying various methods like PDO and MySQL, the data is not getting deleted, but it shows as if it has been deleted. Can someone please help me identify what might be wrong with my code? ...

Steps for implementing AJAX to display a success function and update database results in real-time

I'm struggling with allowing my AJAX call to send data to my PHP file and update the page without a reload. I need the success message to display after approving a user, but their name doesn't move on the page until I refresh. The goal is to app ...

The function to set an attribute value in JavaScript is not reflecting in the DOM

WebSocketd is being used to send and retrieve data from STDOUT, with JS handling the message retrieval: var ws = new WebSocket('ws://ip-address:8081/'); ws.onopen = function() { document.getElementById("phone").style.background= &a ...