dayjournal memo

Total 975 articles!!

Leaflet #010 – マーカーのアイコンを変更

Yasunori Kirimoto's avatar

Leafletでマーカーのアイコンを変更するためには下記のように記述します。

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>
    <script src="http://maps.google.com/maps/api/js?v=3.2&amp;sensor=false"></script>
    <script src="http://matchingnotes.com/javascripts/leaflet-google.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%;
        }

        .leaflet-map-pane {
            z-index: 2 !important;
        }

        .leaflet-google-layer {
            z-index: 1 !important;
        }

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_b = {
    "地理院地図 標準": t_std,
    "地理院地図 淡色": t_pale,
    "地理院地図 オルソ": t_ort,
    "OpenStreetMap 標準": o_std,
    "GoogleMap 標準": g_roadmap,
    "GoogleMap オルソ": g_satellite,
    "GoogleMap ハイブリッド": g_hybrid,
};

var map = L.map('map', {
    center: [35.710063, 139.8107],
    zoom: 16,
    layers: [t_std]
});

var sampleIcon = L.icon({
    iconUrl: 'sample.png',
    iconRetinaUrl: 'sample.png',
    iconSize: [50, 50],
    iconAnchor: [25, 50],
    popupAnchor: [0, -50],
});

var mapMarker = L.marker([35.710063, 139.8107], { icon: sampleIcon }).addTo(map);
var comment = 'sample <br> スカイツリー';
mapMarker.bindPopup(comment).openPopup();

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

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

index.htmlを実行すると下記のようにブラウザで表示されます。 ※「この地図は、国土地理院長の承認を得て、同院発行の電子地形図(タイル)を複製したものである。 (承認番号 平27情複、 第224号)」

Leaflet_010_01


表示させるアイコンを変更したい場合: var sampleIcon~の部分の記述を変更します。

iconUrl: 画像ファイルパス iconRetinaUrl: モバイル用画像ファイルパス iconSize: アイコンのサイズ iconAnchor: 画像のオフセット位置 popupAnchor: ポップアップの表示位置


var sampleIcon = L.icon({
    iconUrl: 'sample2.png', 
    iconRetinaUrl: 'sample2.png',
    iconSize: [25, 25],
    iconAnchor: [12, 25],
    popupAnchor: [0, -25],
});


book

Q&A