dayjournal memo

Total 975 articles!!

Leaflet #003 – 背景地図(OpenStreetMap)の表示

Yasunori Kirimoto's avatar

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

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>

    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
    <script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
    <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');

L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '&amp;copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
    maxZoom: 18
}).addTo(map);

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

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

Leaflet_003_01


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

日本全体表示


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


book

Q&A