公開日:2020.01.31 更新日:2025.02.07

Displaying MapFan API maps with Leaflet

X Facebook LinkedIn

To display an interactive map on a webpage that responds to mouse or tap operations, you need to embed a JavaScript map library into the page.

MapFan A development kit for web systems that allows you to display maps with the same design on a website, and includes functions for location search and route planning: MapFan API However, we have prepared JavaScript libraries, and here are some well-known open-source JavaScript libraries: Leaflet.js Now, let me show you how to display a map using MapFan API.

[For Developers] Download the MapFan API specification here.

What is Leaflet.js?

Leaflet.js is one of the most well-known open-source JavaScript mapping libraries.
It has the following characteristics and has a proven track record of use in many commercial services.

  • Supports a wide range of browsers, including mobile devices.
  • With a simple interface, development is relatively easy.
  • It also has a wide range of plugins and is highly extensible.
  • The license is "BSD-2-Clause".

This article will show you how to display a map obtained from MapFan API using Leaflet, following the steps outlined below.

Step 1: HTML preparation
Step 2: Implementing Authentication
Step 3: Implementing map display

Please note that inquiries regarding the use of Leaflet are outside the scope of our technical support.

[For Developers] Download the MapFan API specification here.

[Step 1] HTML preparation

Here, in order to complete the map display using only a single HTML file, the CSS and JavaScript code will be written within the HTML file.
First, prepare the base HTML as follows. Create a div tag for the area where you want to display the map and assign an ID to it.
The CSS is set to display the map in full screen.

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title>MapFan API Leafletサンプル</title>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin=""/>
    <style type="text/css">
        html, body {
            height: 100%;
            width: 100%;
            margin: 0;
            padding: 0;
        }
        #map {
            width: 100%;
            height: 100%;
        }
        .copyright {
            font-family: 'Meiryo UI';
        }
    </style>
</head>
<body>
    <div id='map'></div>
    <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js" integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew==" crossorigin=""></script>
    <script>
        // ここに処理を実装
    </script>
</body>
</html>

Leaflet's CSS and JS will be loaded from the CDN provided on the official website.

[Step 2] Implementing Authentication

Next, we will implement the authentication process.
We call the 'Authentication API: auth' to obtain the access key.
The parameters for the 'Authentication API' are as follows:

https://api-auth.mapfan.com/v1/auth?

Parameters name explanation
Appid Authentication ID We will issue one to each customer.
date Current time at the time of use Specify it in the format "yyyyMMddhhmmss".
Callback Callback function name used in JSONP If no format is specified, the response will be returned in JSON format.

The source code is as follows:
The results of the authentication API will be received in JSONP format.

var authId = '【認証ID】'
var authUrl = 'https://api-auth.mapfan.com/v1/auth?';

var date = new Date();
var now = zeroPadding(date.getFullYear(), 4) + zeroPadding(date.getMonth() + 1, 2) + zeroPadding(date.getDate(), 2) + zeroPadding(date.getHours(), 2) + zeroPadding(date.getMinutes(), 2) + zeroPadding(date.getSeconds(), 2);

var script = document.createElement('script');
script.src = authUrl + 'appid=' + authId + '&date=' + now + '&callback=authCallback';
document.head.appendChild(script);

// ゼロパディング
function zeroPadding(number, digit) {
    return (Array(digit).join('0') + number).slice(-digit);
}

// 認証APIのコールバック関数
function authCallback(result) {
    if(result.status === 'success') {
        // ここに地図表示処理を実装
    }
}

In the above example, the authentication ID is directly embedded in the source code. However, if you are using this in a public service, please manage the authentication ID to prevent leakage and consider a system configuration where the server-side calls the authentication API and sends only the obtained access key to the page.

[Step 3] Implementing map display

Finally, we will implement the map display process.
By setting the URL of the "Map Image Acquisition API: map" in Leaflet, Leaflet will dynamically request and display a map of the area currently displayed on the screen.
The typical parameters for the 'Map Image Acquisition API' are as follows:

https://api-map.mapfan.com/v1/map?

Parameters name explanation
key Access Key This is the access key obtained from the 'Authentication API'.
tilematrix Tile Matrix Specify it in the format “EPSG code:scale value”.
The EPSG code is either "EPSG:3857" or "EPSG:900913".
The scale value is represented by "{z}".
tilecol WMTS tile number
(Longitude direction)
Specify it in the format "{x}".
tilerow WMTS tile number
(Latitude direction)
Specify it in the format "{y}".

The code should be placed within the "authentication API callback function" mentioned in the authentication process described earlier.

// 認証APIのコールバック関数
function authCallback(result) {
    if(result.status === 'success') {
        var mapUrl = 'https://api-map.mapfan.com/v1/map?';

        // LeafletのMapクラスに、地図を表示する領域のdivタグのidを指定
        // 初期表示位置・スケールは、東京駅 スケール17に設定
        var map = L.map('map').setView([35.68116552628331, 139.76719388020982], 17);

        // 地図画像取得APIを、LeafletのTileLayerクラスのURLに指定
        L.tileLayer(mapUrl + 'key=' + result.key + '&tilematrix=EPSG:3857:{z}&tilecol={x}&tilerow={y}&', {
            maxZoom: 21,
            minZoom: 6,
            attribution: '<span class="copyright">&copy GeoTechnologies, Inc.</span> <a href="https://api-auth.mapfan.com/terms/mfapi_terms.pdf">利用規約</a>'
        }).addTo(map);

        // スケーラーを表示
        L.control.scale({ maxWidth: 200, position: 'bottomleft', imperial: false }).addTo(map);
    }
}

When displayed, it looks like this.

summary

The source code up to this point can be summarized as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8' />
    <title>MapFan API Leafletサンプル</title>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin=""/>
    <style type="text/css">
        html, body {
            height: 100%;
            width: 100%;
            margin: 0;
            padding: 0;
        }
        #map {
            width: 100%;
            height: 100%;
        }
        .copyright {
            font-family: 'Meiryo UI';
        }
    </style>
</head>
<body>
    <div id='map'></div>
    <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js" integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew==" crossorigin=""></script>
    <script>
        var authId = '【認証ID】';
        var authUrl = 'https://api-auth.mapfan.com/v1/auth?';

        var date = new Date();
        var now = zeroPadding(date.getFullYear(), 4) + zeroPadding(date.getMonth() + 1, 2) + zeroPadding(date.getDate(), 2) + zeroPadding(date.getHours(), 2) + zeroPadding(date.getMinutes(), 2) + zeroPadding(date.getSeconds(), 2);

        var script = document.createElement('script');
        script.src = authUrl + 'appid=' + authId + '&date=' + now + '&callback=authCallback';
        document.head.appendChild(script);

        // ゼロパディング
        function zeroPadding(number, digit) {
            return (Array(digit).join('0') + number).slice(-digit);
        }

        // 認証API コールバック
        function authCallback(result) {
            if(result.status === 'success') {
                var mapUrl = 'https://api-map.mapfan.com/v1/map?';

                // LeafletのMapクラスに、地図を表示する領域のdivタグのidを指定
                // 初期表示位置・スケールは、東京駅 スケール17に設定
                var map = L.map('map').setView([35.68116552628331, 139.76719388020982], 17);

                // 地図画像取得APIを、LeafletのTileLayerクラスのURLに指定
                L.tileLayer(mapUrl + 'key=' + result.key + '&tilematrix=EPSG:3857:{z}&tilecol={x}&tilerow={y}&', {
                    maxZoom: 21,
                    minZoom: 6,
                    attribution: '<span class="copyright">&copy GeoTechnologies, Inc.</span> <a href="https://api-auth.mapfan.com/terms/mfapi_terms.pdf">利用規約</a>'
                }).addTo(map);

                // スケーラーを表示
                L.control.scale({ maxWidth: 200, position: 'bottomleft', imperial: false }).addTo(map);
            }
        }
    </script>
</body>
</html>

I've created a page with an embedded animated map that's just under 70 lines of code. By replacing the [Authentication ID] on line 28 with the one issued to you, you can test it on your own browser.

————————————————————————————————————————

This time, we introduced a simple method for displaying maps. MapFan API also offers other APIs with the following features:

  • Get a non-scrolling, static map image.
  • Obtaining latitude and longitude from addresses or place names (geocoding)
  • Obtaining an address from latitude and longitude (reverse geocoding)
  • Get route information by car/on foot
  • Japanese Geodetic System ↔ World Geodetic System Conversion

Furthermore, the 'Map Image Acquisition API' also provides parameters to control the objects displayed on the map and to change the map's design.
I'll provide more details and instructions on how to use them on another occasion.

For inquiries about GeoTechnologies, Inc.' map API MapFan API please contact us here.

[For Developers] Download the MapFan API specification here.

 

Inquiries regarding services for corporate clients
For inquiries, please click here.

Related articles

For requests for brochures or information about our services,
please feel free to contact us using the form below.
​