dayjournal memo

Total 974 articles!!

Leaflet #031 – ラインを表示

Yasunori Kirimoto's avatar

Leafletでラインを表示するには「L.polyline」を利用します。


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" />

    <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 m_mono = new L.tileLayer('https://tile.mierune.co.jp/mierune_mono/{z}/{x}/{y}.png', {
    attribution: "Maptiles by <a href='http://mierune.co.jp/' target='_blank'>MIERUNE</a>, under CC BY. Data by <a href='http://osm.org/copyright' target='_blank'>OpenStreetMap</a> contributors, under ODbL."
});

var m_color = new L.tileLayer('https://tile.mierune.co.jp/mierune/{z}/{x}/{y}.png', {
    attribution: "Maptiles by <a href='http://mierune.co.jp/' target='_blank'>MIERUNE</a>, under CC BY. Data by <a href='http://osm.org/copyright' target='_blank'>OpenStreetMap</a> contributors, under ODbL."
});

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 map = L.map('map', {
    center: [35.681,139.763],
    zoom: 14,
    zoomControl: true,
    layers: [m_mono]
});

var Map_BaseLayer = {
    "MIERUNE地図 color": m_color,
    "MIERUNE地図 mono": m_mono,
    "地理院地図 淡色": t_pale,
    "地理院地図 オルソ": t_ort,
    "OpenStreetMap 標準": o_std
};

var Line = L.polyline([
    [35.66413037753069,139.75278854370114],
    [35.67214927327908,139.76523399353027],
    [35.68888176518044,139.77107048034668],
    [35.69703758268826,139.76660728454587]
],{
    "color": "#FF0000",
    "weight": 5,
    "opacity": 0.6
}).addTo(map);

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

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

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

example


スタイルを変更:


var Line = L.polyline([
    [35.66413037753069,139.75278854370114],
    [35.67214927327908,139.76523399353027],
    [35.68888176518044,139.77107048034668],
    [35.69703758268826,139.76660728454587]
],{
    "color": "#2FFF00",
    "weight": 10,
    "opacity": 0.2
}).addTo(map)

L.polylineを利用すると地図上にラインを表示することができます。



book

Q&A


- 参考文献 [Leaflet](http://leafletjs.com)