公開日:2020.02.28 更新日:2025.01.17

Displaying MapFan API maps as layers in Leaflet

X Facebook LinkedIn

In the previous article, I introduced a simple method for displaying a MapFan API map using Leaflet.
This time, we will introduce how to use Leaflet's layer function to switch between displaying maps with different designs and how to display customer-owned data on the map.

[For Developers] Download the MapFan API specification here.

Layer types

Leaflet allows you to use base layers and overlay layers.

The base layer is the layer displayed at the very bottom of the map view.
By registering multiple base layers, you can switch between maps with different designs, but only one layer can be displayed at a time.

An overlay layer is a layer that is displayed on top of the base layer, and it is possible to display multiple overlay layers simultaneously.
When highlighting points or areas on a map using shapes such as points, polylines, polygons, or GeoJSON, managing these elements becomes easier. For example, by separating the overlay layers registered according to the content and type of the displayed element, it becomes possible to turn them on or off in groups.

Furthermore, by registering the layers of the displayed objects you create in Control.Layers, Leaflet provides a UI controller that allows you to control their display.
The base layer is toggled using radio buttons, while the overlay layer is displayed or disabled using checkboxes.
The default appearance is as shown in the image below, but the design can be changed using CSS.

Base layer (WMTS tile)

Now, let's discuss the implementation of the base layer.
The implementation will be done within the callback of the authentication API described in the previous article.

The base layer implementation process is as follows:

●1. Creating a Layer
●2. Set the Layer to be displayed initially from (1) to Map.
●3. Link the display name on the UI controller to the Layer in (1).
●4. Set (3) to L.control.layers, and set L.control.layers to Map.

Here, we'll use the ` mapstyle` parameter of the `map` API to create layers with different map designs and change the appearance of the map.

// ベースレイヤーの表示オプション
var baseOptions = {
    minZoom: 6,
    maxZoom: 21,
    attribution: '<span class="copyright">&copy GeoTechnologies, Inc.</span> <a href="https://api-auth.mapfan.com/terms/mfapi_terms.pdf">利用規約</a>',
};

var mapUrl = 'https://api-map.mapfan.com/v1/map?' + 'key=' + result.key + '&tilematrix=EPSG:3857:{z}&tilecol={x}&tilerow={y}';

// ●1. Layerの生成
// mapstyle=std_pc(標準デザイン(デフォルト))
var standard = L.tileLayer(mapUrl, baseOptions);
// mapstyle=gray_pc(グレースケール地図)
var gray = L.tileLayer(mapUrl + '&mapstyle=gray_pc', baseOptions);
// mapstyle=rpg_pc(RPG風地図)
var rpg = L.tileLayer(mapUrl + '&mapstyle=rpg_pc', baseOptions);
// mapstyle=antique_pc(古地図風地図)
var antique = L.tileLayer(mapUrl + '&mapstyle=antique_pc', baseOptions);
// mapstyle=tourism_pc(おもてなしマップ(カラーユニバーサルデザイン対応))
var tourism = L.tileLayer(mapUrl + '&mapstyle=tourism_pc', baseOptions);

// LeafletのMapクラスに、地図を表示する領域のdivタグのidを指定
// 初期表示位置・スケールは、東京駅 スケール17に設定
var map = L.map('map', {
    center: L.latLng(35.68116552628331, 139.76719388020982),
    zoom: 17,
    // ●2. (1)のうち初期表示するLayerをMapへ設定
    layers: [standard],
});

// ●3. UIコントローラー上の表示名と(1)のLayerを紐付け
var baseLayerControls = {
    '標準': standard,
    'グレー': gray,
    'RPG風': rpg,
    '古地図風': antique,
    'おもてなし': tourism,
};

// ●4. (3)をL.control.layersへ設定、L.control.layersをMapへ設定
L.control.layers(baseLayerControls, null, {
    collapsed: false,
}).addTo(map);

With the implementation up to this point, the screen will look like this.

In addition, MapFan API offers multilingual maps where annotations (such as station names and facility names) can be displayed in 13 languages (14 languages). (A separate optional contract is required.)

[For Developers] Download the MapFan API specification here.

Multilingual maps are used by specifying the language using the ` lang` parameter of the `map` API.
Furthermore, all languages can be displayed alongside Japanese.

// ●1. Layerの生成
// 標準デザイン x 日本語
var ja = L.tileLayer(mapUrl + '&lang=ja', baseOptions);
// 標準デザイン x 英語
var en = L.tileLayer(mapUrl + '&lang=en', baseOptions);
// 標準デザイン x 中国語(簡体字)
var zh2 = L.tileLayer(mapUrl + '&lang=zh2', baseOptions);
// 標準デザイン x 中国語(繁体字)
var zh1 = L.tileLayer(mapUrl + '&lang=zh1', baseOptions);
// 標準デザイン x 韓国語
var ko = L.tileLayer(mapUrl + '&lang=ko', baseOptions);
// 標準デザイン x タイ語
var th = L.tileLayer(mapUrl + '&lang=th', baseOptions);
// 標準デザイン x インドネシア語
var id = L.tileLayer(mapUrl + '&lang=id', baseOptions);
// 標準デザイン x フランス語
var fr = L.tileLayer(mapUrl + '&lang=fr', baseOptions);
// 標準デザイン x ドイツ語
var de = L.tileLayer(mapUrl + '&lang=de', baseOptions);
// 標準デザイン x ベトナム語
var vi = L.tileLayer(mapUrl + '&lang=vi', baseOptions);
// 標準デザイン x スペイン語
var es = L.tileLayer(mapUrl + '&lang=es', baseOptions);
// 標準デザイン x イタリア語
var it = L.tileLayer(mapUrl + '&lang=it', baseOptions);
// 標準デザイン x ロシア語
var ru = L.tileLayer(mapUrl + '&lang=ru', baseOptions);
// 標準デザイン x ポルトガル語
var pt = L.tileLayer(mapUrl + '&lang=pt', baseOptions);
// 標準デザイン x 併記(日本語 - 英語)
var ja_en = L.tileLayer(mapUrl + '&lang=ja,en', baseOptions);

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

// ●3. UIコントローラー上の表示名と(1)のLayerを紐付け
var baseLayerControls = {
    '日本語': ja,
    '英語': en,
    '中国語(簡体字)': zh2,
    '中国語(繁体字)': zh1,
    '韓国語': ko,
    'タイ語': th,
    'インドネシア語': id,
    'フランス語': fr,
    'ドイツ語': de,
    'ベトナム語': vi,
    'スペイン語': es,
    'イタリア語': it,
    'ロシア語': ru,
    'ポルトガル語': pt,
    '併記(日本語・英語)': ja_en,
};

// ●4. (3)をL.control.layersへ設定、L.control.layersをMapへ設定
L.control.layers(baseLayerControls, null, {
    collapsed: false,
}).addTo(map);

The deliverables will be as follows:

Overlay layer (WMTS tile)

Next, let's talk about overlay layers.
The procedure is almost the same as for the base layer.

MapFan API is a 'Map Image Acquisition API: map'. target parameter Using this method, The map is divided into background and annotation layers. It is possible to do so.

As mentioned above, the "RPG-style map" and "antique map-style map" do not have dedicated foreign language fonts for each design, so we cannot provide multilingual maps with integrated backgrounds and annotations. However, by obtaining the annotations from the standard map design and overlaying them as an overlay layer, you can display a "specially designed map with multilingual features."

// ●1. Layerの生成
// [ベースレイヤー] mapstyle=antique_pc(古地図風地図)(背景のみ)
var backAntique = L.tileLayer(mapUrl + '&target=1' + '&mapstyle=antique_pc', baseOptions);

// [オーバーレイレイヤー] 標準デザイン x 日本語(注記のみ)
var labelJa = L.tileLayer(mapUrl + '&target=2' + '&lang=ja', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x 英語(注記のみ)
var labelEn = L.tileLayer(mapUrl + '&target=2' + '&lang=en', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x 中国語(簡体字)(注記のみ)
var labelZh2 = L.tileLayer(mapUrl + '&target=2' + '&lang=zh2', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x 中国語(繁体字)(注記のみ)
var labelZh1 = L.tileLayer(mapUrl + '&target=2' + '&lang=zh1', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x 韓国語(注記のみ)
var labelKo = L.tileLayer(mapUrl + '&target=2' + '&lang=ko', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x タイ語(注記のみ)
var labelTh = L.tileLayer(mapUrl + '&target=2' + '&lang=th', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x インドネシア語(注記のみ)
var labelId = L.tileLayer(mapUrl + '&target=2' + '&lang=id', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x フランス語(注記のみ)
var labelFr = L.tileLayer(mapUrl + '&target=2' + '&lang=fr', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x ドイツ語(注記のみ)
var labelDe = L.tileLayer(mapUrl + '&target=2' + '&lang=de', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x ベトナム語(注記のみ)
var labelVi = L.tileLayer(mapUrl + '&target=2' + '&lang=vi', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x スペイン語(注記のみ)
var labelEs = L.tileLayer(mapUrl + '&target=2' + '&lang=es', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x イタリア語(注記のみ)
var labelIt = L.tileLayer(mapUrl + '&target=2' + '&lang=it', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x ロシア語(注記のみ)
var labelRu = L.tileLayer(mapUrl + '&target=2' + '&lang=ru', baseOptions);
// [オーバーレイレイヤー] 標準デザイン x ポルトガル語(注記のみ)
var labelPt = L.tileLayer(mapUrl + '&target=2' + '&lang=pt', baseOptions);

// LeafletのMapクラスに、地図を表示する領域のdivタグのidを指定
// 初期表示位置・スケールは、東京駅 スケール17に設定
var map = L.map('map', {
    center: L.latLng(35.68116552628331, 139.76719388020982),
    zoom: 17,
    // ●2. 初期表示するLayerをMapへ設定
    layers: [backAntique, labelJa],
});

// ●3. [ベースレイヤー] UIコントローラー上の表示名と(1)のLayerを紐付け
var baseLayerControls = {
    '古地図風(背景のみ)': backAntique,
};

// ●3. [オーバーレイレイヤー] UIコントローラー上の表示名と(1)のLayerを紐付け
var overlayLayerControls = {
    '日本語(注記のみ)': labelJa,
    '英語(注記のみ)': labelEn,
    '中国語(簡体字)(注記のみ)': labelZh2,
    '中国語(繁体字)(注記のみ)': labelZh1,
    '韓国語(注記のみ)': labelKo,
    'タイ語(注記のみ)': labelTh,
    'インドネシア語(注記のみ)': labelId,
    'フランス語(注記のみ)': labelFr,
    'ドイツ語(注記のみ)': labelDe,
    'ベトナム語(注記のみ)': labelVi,
    'スペイン語(注記のみ)': labelEs,
    'イタリア語(注記のみ)': labelIt,
    'ロシア語(注記のみ)': labelRu,
    'ポルトガル語(注記のみ)': labelPt,
};

// ●4. (3)をL.control.layersへ設定、L.control.layersをMapへ設定
L.control.layers(baseLayerControls, overlayLayerControls, {
    collapsed: false,
}).addTo(map);

The deliverables will be as follows:

Overlay layer (shape)

The overlay layer can also include layers other than the WMTS-distributed map tiles that we have introduced so far.
Leaflet's built-in "Marker" for displaying locations as icons, "Polyline/Polygon" for indicating areas, and "GeoJson" which can be imported from external files, are all available because they inherit from the Layer class.

The latitude and longitude information for the objects shown below has been extracted and processed from MapFan DB, our map database product.
Please note that this data and information is not available through MapFan API.

// [ベースレイヤー] MapFan API 標準デザイン
var standard = L.tileLayer(mapUrl, baseOptions);

// ●1. Layerの生成
// [オーバーレイレイヤー] 山手線 駅
var shibuya = L.marker(/* 渋谷駅の緯度経度 */);
var harajuku = L.marker(/* 原宿駅の緯度経度 */);
var yoyogi = L.marker(/* 代々木駅の緯度経度 */);
var shinjuku = L.marker(/* 新宿駅の緯度経度 */);
// [オーバーレイレイヤー] 山手線 駅一覧
var yamanoteStations = L.layerGroup(/* 作成した山手線 駅の配列([shibuya, harajuku, yoyogi, shinjuku...])*/)

// [オーバーレイレイヤー] 山手線 線路
var yamanoteLine = L.polyline(/* 山手線 線路形状の緯度経度点列 */)

// LeafletのMapクラスに、地図を表示する領域のdivタグのidを指定
// 初期表示位置・スケールは、東京駅 スケール17に設定
var map = L.map('map', {
    center: L.latLng(35.68116552628331, 139.76719388020982),
    zoom: 17,
    // ●2. (1)のうち初期表示するLayerをMapへ設定
    // オーバーレイレイヤーは、いずれも初期表示なし
    layers: [standard],
});

// ●3. [オーバーレイレイヤー] UIコントローラー上の表示名と(1)のLayerを紐付け
var overlayLayerControls = {
    '山手線(駅)': yamanoteStations,
    '山手線(線路)': yamanoteLine,
};

// ●4. (3)をL.control.layersへ設定、L.control.layersをMapへ設定
// ベースレイヤーは固定・UI表示不要なため、nullを設定
L.control.layers(null, overlayLayerControls, {
    collapsed: false,
}).addTo(map);

// 地図表示範囲を、山手線 線路領域にフィット
map.fitBounds(yamanoteLine.getBounds());

The deliverables will be as follows:

The following is an example of reading and displaying GeoJSON data prepared in an external file.
Here, GeoJSON is loaded using jQuery's $.getJSON method.

// GeoJsonファイルの読み込み
$.getJSON('GeoJsonファイルのパス', function(data) {
    // ●1. Layerの生成
    var geoJson = L.geoJson(data);

    // LeafletのMapクラスに、地図を表示する領域のdivタグのidを指定
    // 初期表示位置・スケールは、東京駅 スケール17に設定
    var map = L.map('map', {
        center: L.latLng(35.68116552628331, 139.76719388020982),
        zoom: 6,
        // ●2. (1)のうち初期表示するLayerをMapへ設定
        layers: [standard],
    });

    // ●3. UIコントローラー上の表示名と(1)のLayerを紐付け
    var overlayLayerControls = {
        'GeoJson': geoJson,
    };

    // ●4. (3)をL.control.layersへ設定、L.control.layersをMapへ設定
    L.control.layers(null, overlayLayerControls, {
        collapsed: false,
    }).addTo(map);

    // L.control.layers上でオーバーレイレイヤーの表示操作が行われた際、地図表示範囲をオーバーレイレイヤー領域にフィット
    map.on('overlayadd', (layer) => {
        map.fitBounds(layer.layer.getBounds())
    })
});

The deliverables will be as follows:
*Based on the source code above, we have also implemented features such as loading multiple GeoJSON files, changing the design of shapes, and configuring popup display settings.

summary

In this article, we introduced various map display methods using MapFan API and our database product, MapFan DB.
We hope this will be helpful when you want to display your own location information in Leaflet.

Next time, I'd like to introduce features of MapFan API other than map display, such as search and route planning.

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

Download the MapFan API specification here.

Click here for details on MapFan API

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.
​