How to display the geolocation results on amChart map

There is a growing number of websites that display various geolocation information using an interactive map object, for example, an amChart. A well-designed map display could provide visitors a quick glance of important information. For example, a statistical company could use the map to illustrate the service utilization rates globally, or a disease control department could display a real-time epidemic outbreak clearly on a map, and so on.

In this article, we will demonstrate how to map the countries using the IP2Location geolocation database based on IP addresses.

  1. In this example, we will pre-define a set of IP-countries information for the map highlighting. Please note that in reality, this information should be retrieved from a source, such as database, real-time web data, user input and so on.
    var myJSON = [
                "IP_Number": "16909059",
                "IP_Version": 4,
                "IP_Address": "",
                "Country_Code": "AU",
                "Country_Name": "Australia",
                "Region_Name": "Queensland",
                "City_Name": "Brisbane"
                "IP_Number": "33907478",
                "IP_Version": 4,
                "IP_Address": "",
                "Country_Code": "FR",
                "Country_Name": "France",
                "Region_Name": "Hauts-de-France",
                "City_Name": "Lille"
                "IP_Number": "553784885",
                "IP_Version": 4,
                "IP_Address": "",
                "Country_Code": "US",
                "Country_Name": "United States of America",
                "Region_Name": "Ohio",
                "City_Name": "Columbus"
                "IP_Number": "721947651",
                "IP_Version": 4,
                "IP_Address": "",
                "Country_Code": "JP",
                "Country_Name": "Japan",
                "Region_Name": "Tokyo",
                "City_Name": "Tokyo"
                "IP_Number": "891958082",
                "IP_Version": 4,
                "IP_Address": "",
                "Country_Code": "DE",
                "Country_Name": "Germany",
                "Region_Name": "Baden-Wurttemberg",
                "City_Name": "Stuttgart"
  2. Next, create a new project and insert the below codes:
    i) Add a function to display the highlight of the countries that is returned by the IP2Location IP lookup into the JavaScript code(countryHighlightJS.js)

    am4core.ready(function () {
         var myJSON = […];
        // Themes begin
        // Themes end
        var continents = {
            "AF": 0,
            "AN": 1,
            "AS": 2,
            "EU": 3,
            "NA": 4,
            "OC": 5,
            "SA": 6
        // Create map instance
        var chart = am4core.create("chartdiv", am4maps.MapChart);
        chart.projection = new am4maps.projections.Miller();
        // Create map polygon series for world map
        var worldSeries = chart.series.push(new am4maps.MapPolygonSeries());
        worldSeries.useGeodata = true;
        worldSeries.geodata = am4geodata_worldLow;
        worldSeries.exclude = ["AQ"];
        var worldPolygon = worldSeries.mapPolygons.template;
        worldPolygon.tooltipText = "{name}";
        worldPolygon.nonScalingStroke = true;
        worldPolygon.strokeOpacity = 0.5;
        worldPolygon.fill = am4core.color("#eee");
        worldPolygon.propertyFields.fill = "color";
        // Create country specific series (but hide it for now)
        var countrySeries = chart.series.push(new am4maps.MapPolygonSeries());
        countrySeries.useGeodata = true;
        countrySeries.hide();"done", function (ev) {
        var countryPolygon = countrySeries.mapPolygons.template;
        countryPolygon.tooltipText = "{name}";
        countryPolygon.nonScalingStroke = true;
        countryPolygon.strokeOpacity = 0.5;
        countryPolygon.fill = am4core.color("#eee");
        var hs = countryPolygon.states.create("hover"); = chart.colors.getIndex(9);
        // Set up click events"hit", function (ev) {
            var map =;
            var mapID =;
            if (map) {
       = false;
                countrySeries.geodataSource.url = "" + map + ".json";
        // Set up data for countries
        var data = [];
        for (var id in am4geodata_data_countries2) {
            if (am4geodata_data_countries2.hasOwnProperty(id)) {
                var country = am4geodata_data_countries2[id];
                for (var i in myJSON) {
                    if (id == myJSON[i].Country_Code) {
                            id: id,
                            color: chart.colors.getIndex(continents[country.continent_code]),
                            map: country.maps[0]
        } = data;
        // Zoom control
        chart.zoomControl = new am4maps.ZoomControl();
        var homeButton = new am4core.Button();"hit", function () {
        homeButton.icon = new am4core.Sprite();
        homeButton.padding(7, 5, 7, 5);
        homeButton.width = 30;
        homeButton.icon.path = "M16,8 L14,8 L14,16 L10,16 L10,10 L6,10 L6,16 L2,16 L2,8 L0,8 L8,0 L16,8 Z M16,8";
        homeButton.marginBottom = 10;
        homeButton.parent = chart.zoomControl;
    }); // end am4core.ready()

    ii) Add the amCharts resources link, style for the map and the division ‘chartdiv’

    <!DOCTYPE html>
        <meta charset="UTF-8">
        <title>World Map with highlight</title>
        <!-- Styles -->
            #chartdiv {
                width: 100%;
                height: 500px;
        <!-- Resources -->
        <script src=""></script>
        <script src=""></script>
        <script src=""></script>
        <script src=""></script>
        <script src=""></script>
        <script src="//"></script>
        <script src="countryHighlightJS.js"></script>
        <div id="chartdiv"></div>
  3. After all the steps are completed, you will be able to see the highlighted countries. If you click on the highlighted country, you will be able to view more details.
    How to display the geolocation results on amChart mapHow to display the geolocation results on amChart map


Please note that if you follow the example above, the country codes for “AU”, “FR”, “US”, “JP”, “DE” will be highlighted only.

Was this article helpful?

Related Articles