dayjournal memo

Total 975 articles!!

Leaflet #011 – 背景地図(BingMap)の表示

Yasunori Kirimoto's avatar

Leafletで背景にBingMapを表示するためには準備作業が2つ必要です。

①BingMapを表示するためにkeyを作成します。 まず、「bing Maps Dev Center」にアクセスします。

Microsoft accountでログインし、Maps Dev Centerに登録をします。

登録が完了したらMY account → Create or view keys

Leaflet_011_03

keyの登録情報を記入して実行します。 ※Key typeは「Basic」にします。

Leaflet_011_02

実行されるとkeyが作成されるのでコード内の「取得したKeyを入力」に貼り付けます。


②Leafletのプラグインの1つである「leaflet-plugins」を使用します。 プラグインをダウンロードしたらその中にある「Bing.js」を読み込みます。

    <script src="./plugin/leaflet-plugins-master/layer/tile/Bing.js"></script>


Leafletで背景にBingMapを表示するためには下記のように記述します。

index.html


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>sample</title>

    <script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
    <script src="./plugin/leaflet-plugins-master/layer/tile/Bing.js"></script>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
    <link href="stylesheet.css" rel="stylesheet" />

</head>

<body>

    <div id="map"></div>

    <script src="script.js"></script>

</body>

</html>

stylesheet.css


        html, body {
            width: 100%;
            height: 100%;

        }

        #map {
            width: 100%;
            height: 100%;
        }

script.js


var map = L.map('map');

var BingMap = new L.BingLayer("取得したKeyを入力", { type: 'Road' });
map.addLayer(BingMap);

map.setView([35.681382, 139.766084], 15);

index.htmlを実行すると下記のようにブラウザで表示されます。


読み込む地図の種類を変えたい時: L.BingLayer~の部分の記述を変更します。

BingMapオルソ


var BingMap = new L.BingLayer("取得したKeyを入力", { type: 'Aerial' });
map.addLayer(BingMap);

初期表示位置・縮尺を変えたい時: map.setView~の部分の記述を変更します。

日本全体表示


map.setView([35.999887, 138.75], 5);


book

Q&A