Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a map of Canada and USA with Datamaps in a single page

Tags:

datamaps

I am using Datamaps to create a map of Canada and USA. I saw the tutorial and/or examples in its website and I saw a "USA map only" example. And I did that:

    <script>
        var addUSA = new Datamap({
            scope: 'usa',
            element: document.getElementById('usa-map'),
            geographyConfig: {
                highlightOnHover: false,
                borderColor: '#006298',
                borderWidth: 0.8,
                popupTemplate: function(geography, data) {
                    return "<div class='hoverinfo'><strong>" + data.info + "</strong></div>";
                }
            },
            dataUrl: 'data.json',
            dataType: 'json',
            data: {},
            fills: {
                defaultFill: '#FFFFFF'
            }
        });
        addUSA.labels();
    </script>

So I assume that you can also create a "Canada map only". But the problem is, I don't know how to combine two countries.

I aim for labels, the hover-info and json that's why I'm using Datamaps.

like image 398
Hello World Avatar asked Sep 28 '16 09:09

Hello World


1 Answers

So I've found this URL entitled Custom Map Data in Datamaps by Mark DiMarco and I used and tried copying what he had done. On that link, he created a map of Afghanistan which was not included in his main examples on Datamaps website. But instead of one country, we will combine two countries custom map using Datamaps. This is an experiment I've made but I hope this will be the answer to your problem

First, he created a custom topo json for Afghanistan. He published a tutorial on how to create custom map data but I think I don't have an access 'cause I'm getting 404 or he took it down. Going back, the code he used for that custom topo json can also be found in his other works located at "More Versions" link in Datamaps website. You just need to look for the country/ies you need to make a custom topo json. On your end, look for datamaps.afg.js and datamaps.usa.js; and get the json.

I only have 1 reputation and I am limit with two URLs. Just visit this GitHub site where I put those two custom topo json for Canada and USA.


HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>Canada and USA</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="http://d3js.org/topojson.v1.min.js"></script>
    <script src="http://rawgithub.com/markmarkoh/datamaps/master/dist/datamaps.none.min.js"></script>
    <script src="script.js"></script>
</head>
<body>
    <!-- CANADA -->
    <h1>CANADA</h1>
    <div id="canada"></div>
    <!-- USA -->
    <h1>USA</h1>
    <div id="usa"></div>
</body>
</html>

CSS

#canada {
    border: 1px solid #000000;
    height: 450px;
    width: 400px;
}

#usa {
    border: 2px solid #EDA552;
    height: 400px;
    width: 500px;
}

JQUERY

$(function() {
    var canadaMap = new Datamap({
        element: document.getElementById('canada'),
        geographyConfig: {
            dataUrl: 'canada.topo.json'
        },
        scope: 'canada',
        fills: {
            defaultFill: '#bada55'
        },
        setProjection: function(element) {
            var projection = d3.geo.mercator()
                .center([-95, 71])
                .scale(200)
                .translate([element.offsetWidth / 2, element.offsetHeight / 2]);
            var path = d3.geo.path().projection(projection);
            return {path: path, projection: projection};
        }
    });
    var USAmap = new Datamap({
        element: document.getElementById('usa'),
        geographyConfig: {
            dataUrl: 'usa.topo.json'
        },
        scope: 'usa',
        fills: {
            defaultFill: '#bada55'
        },
        setProjection: function(element) {
            var projection = d3.geo.mercator()
                .center([-120, 54])
                .scale(250)
                .translate([element.offsetWidth / 2, element.offsetHeight / 2]);
            var path = d3.geo.path().projection(projection);
            return {path: path, projection: projection};
        }
    });
});

Working code here => JS FIDDLE

like image 109
kirenariyu Avatar answered Oct 19 '22 18:10

kirenariyu