dayjournal memo

Total 974 articles!!

Leaflet #029 – パルスマーカー

Yasunori Kirimoto's avatar

Leafletでパルスマーカーを実装するには、「leaflet-icon-pulse」と言うプラグインを利用すると便利です。


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="./plugin/leaflet-icon-pulse/src/L.Icon.Pulse.js"></script>
    <link href="./plugin/leaflet-icon-pulse/src/L.Icon.Pulse.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.682,139.759],
    zoom: 14,
    zoomControl: true,
    layers: [m_color]
});

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

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

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

var pulsingIcon = L.icon.pulse({
    iconSize:[25,25],
    color:'red',
    animate: true,
    heartbeat: 1
});

var marker = L.marker(
    [35.681,139.767],
    {icon: pulsingIcon}
).addTo(map)

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

example


アニメーションをOFF:


var pulsingIcon = L.icon.pulse({
    iconSize:[25,25],
    color:'red',
    animate: false,
    heartbeat: 1
})

パルスの間隔を設定:


var pulsingIcon = L.icon.pulse({
    iconSize:[25,25],
    color:'red',
    animate: true,
    heartbeat: 5
})

パルスマーカーを実装したい時に便利なプラグインです。



book

Q&A