勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

Leafletで「leaflet.sprite」プラグインを導入してマーカーの色を変更する方法

  JavaScript

LeafletとOpenStreetMapでマップ上にマーカーを複数設置したい。また、マーカーはそれぞれ色を変更したい。調べたところ「leaflet.sprite」プラグインを導入することで容易に実装できた。以下にサンプル及びソースコードをメモ。

 

マーカー色指定ではなく画像を個別に設定したい場合

画像を個別に設定したい場合は以下過去記事参照。

https://taitan916.info/blog/archives/3184

 

leaflet.sprite

GitHub

https://github.com/leaflet-extras/leaflet.sprite

CDN

CDNで使う場合は以下を記述する。

<script src="https://cdn.jsdelivr.net/npm/leaflet-sprite@1.0.0/dist/leaflet.sprite.min.js"></script>

 

サンプル

https://taitan916.info/sample/leaflet.js/plugin/leaflet.sprite/

マーカーが7色指定されている点が確認できる筈。

 

実装方法

ソースコード

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

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.js"></script>
<script src="https://cdn.jsdelivr.net/npm/leaflet-sprite@1.0.0/dist/leaflet.sprite.min.js"></script>
<script>
let map;
map = L.map('map');

//中心の経度緯度
const center = ['35.681236', '139.767125'];

//ズームレベル
const zoom = 12;

//マップの中心地とズームレベルを指定
map.setView(center, zoom);

const tileLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
    attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a>',
    maxZoom: 19
});
tileLayer.addTo(map);

//マーカー生成
const mapData = [
    { 'lat' : '35.689607', 'lng' : '139.700571', 'body' : '新宿駅', }, 
    { 'lat' : '35.681236', 'lng' : '139.767125', 'body' : '東京駅', 'icon' : 'green'}, 
    { 'lat' : '35.628471', 'lng' : '139.73876', 'body' : '品川駅', 'icon' : 'orange'}, 
    { 'lat' : '35.658581', 'lng' : '139.745433', 'body' : '東京タワー', 'icon' : 'yellow'}, 
    { 'lat' : '35.660238', 'lng' : '139.730077', 'body' : '六本木ヒルズ', 'icon' : 'red'}, 
    { 'lat' : '35.714765', 'lng' : '139.796655', 'body' : '浅草寺', 'icon' : 'purple'}, 
    { 'lat' : '35.636564', 'lng' : '139.763144', 'body' : 'レインボーブリッジ', 'icon' : 'violet'}, 
];
for( key in mapData ){
    makeMarker(mapData[key]);
}

function makeMarker( mapData ){
    L.Icon.Default.imagePath = 'https://unpkg.com/leaflet@1.3.1/dist/images/';
    const icon = L.spriteIcon(mapData.icon);

    //ポップアップの表示位置調整
    icon.options.popupAnchor = [2, -35];
    L.marker([mapData.lat, mapData.lng], {icon: icon})
    .bindPopup(mapData.body)
    .addTo(map);
}
</script>

解説等

mapData配列でiconの色を指定し「L.marker([mapData.lat, mapData.lng], {icon: icon})」部分でマーカーを設定している。

iconの色を指定しない場合(今回のソースコードだと新宿駅の場合)はデフォルトの青色マーカーが設置される。

尚、マーカークリック時のフキダシがデフォルトだと座標の位置に表示されるっぽく、マーカーと重なってしまうため以下で位置を調整している。

const icon = L.spriteIcon(mapData.icon);
icon.options.popupAnchor = [2, -35];

 - JavaScript

  関連記事

Leafletにてマップ移動時にURLに座標を自動付与する「leaflet-view-meta」プラグインの利用方法

Googleマップだとマップを移動した際、URLに座標(経度緯度)が付与されるた ...

leaflet.jsとOSMでマーカーの画像をそれぞれ指定する

leaflet.jsとOpenStreetMapでマップ上にマーカーを複数設置し ...

Leafletでマーカークリック時に当該マーカーをバウンド(跳ねる動作を)させる方法

LeafletとOpenStreetMapでマップ上にマーカーを複数設置した上で ...

Leafletにタグフィルター機能を実装できる「leaflet-tag-filter-button」プラグインの利用方法

以前にLeafletでマーカーをグループ化し、チェックボックスの状態により表示非 ...

Leafletでマーカーを動的に変更し、変更状況をCookieに保存する方法

Leafletでポップアップ(フキダシ)内のボタンをクリックするとマーカーを動的 ...