dayjournal memo

Total 975 articles!!

Leaflet #025 – 距離計測

Yasunori Kirimoto's avatar

Leafletで距離計測するためには、「Leaflet.MeasureControl」と言うプラグインを利用します。

Leaflet.MeasureControlを利用するためには、「Leaflet #024 – ベクトルを作図」で紹介したLeaflet.drawプラグインも読み込む必要があります。


index.html


<!DOCTYPE html>
<html lang="ja">

<head>
    <meta charset="UTF-8">
    <title>Leaflet Sample</title>

    <script src="./library/leaflet-0.7.3/leaflet.js"></script>
    <link href="./library/leaflet-0.7.3/leaflet.css" rel="stylesheet" />

    <script src="http://maps.google.com/maps/api/js?sensor=false&amp;amp;region=JP"></script>
    <script src="./plugin/leaflet-plugins/layer/tile/Google.js"></script>

    <script src="./plugin/Leaflet.draw/dist/leaflet.draw.js"></script>
    <link href="./plugin/Leaflet.draw/dist/leaflet.draw.css" rel="stylesheet" />

    <script src="./plugin/Leaflet.MeasureControl/leaflet.measurecontrol.js"></script>
    <link href="./plugin/Leaflet.MeasureControl/leaflet.measurecontrol.css" rel="stylesheet" />

    <link href="./css/stylesheet.css" rel="stylesheet" />

</head>
<body>

    <div id="map"></div>
    <script src="./js/script.js"></script>

</body>
</html>

stylesheet.css


html, body {
    height: 100%;
    padding: 0;
    margin: 0;
}

#map {
    z-index: 0;
    height: 100%;
}

script.js


var t_std = new L.tileLayer('http://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png', {
    attribution: "<a href='http://www.gsi.go.jp/kikakuchousei/kikakuchousei40182.html' target='_blank'>国土地理院</a>"
});

var t_pale = new L.tileLayer('http://cyberjapandata.gsi.go.jp/xyz/pale/{z}/{x}/{y}.png', {
    attribution: "<a href='http://www.gsi.go.jp/kikakuchousei/kikakuchousei40182.html' target='_blank'>国土地理院</a>"
});

var t_ort = new L.tileLayer('http://cyberjapandata.gsi.go.jp/xyz/ort/{z}/{x}/{y}.jpg', {
    attribution: "<a href='http://www.gsi.go.jp/kikakuchousei/kikakuchousei40182.html' target='_blank'>国土地理院</a>"
});

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

var g_roadmap = new L.Google('ROADMAP');

var g_satellite = new L.Google('SATELLITE');

var g_hybrid = new L.Google('HYBRID');

var map = L.map('map', {
    center: [35.6831925, 139.7511307],
    zoom: 13,
    zoomControl: true,
    layers: [o_std]
});

var Map_BaseLayer = {
    "地理院地図 標準": t_std,
    "地理院地図 淡色": t_pale,
    "地理院地図 オルソ": t_ort,
    "OpenStreetMap 標準": o_std,
    "GoogleMap 標準": g_roadmap,
    "GoogleMap オルソ": g_satellite,
    "GoogleMap ハイブリッド": g_hybrid
};

L.control.scale({
    imperial: false,
    maxWidth: 300
}).addTo(map);

L.control.layers(Map_BaseLayer, null, {
    collapsed: false
}).addTo(map);

L.Control.measureControl().addTo(map)

index.htmlを実行すると下記のようにブラウザで表示されます。左上に距離計測コントロールが表示されます。 leaflet_025_01


距離計測コントロールをクリックすると、実際に地図上の距離を計測することが可能になります。 leaflet_025_02


L.mapの中でも設定可能:


var map = L.map('map', {
    center: [35.6831925, 139.7511307],
    zoom: 13,
    zoomControl: true,
    measureControl:true,
    layers: [o_std]
})

距離計測の機能を実装したい時に便利なプラグインです。使い方もシンプルでわかりやすいです。



book

Q&A