概要
クリック位置に地物(Feature)があれば、その属性をポップアップで表示する。ここでは地理院ベクトルタイルを表示させた地図に地物を表示させ、これらをクリックしたときにその属性を表示させている。
実装例
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | <!DOCTYPE html> <html lang="ja" dir="ltr">   <head>     <meta charset="utf-8">     <title>Test - Popup</title>     <meta name="viewport" content="width=device-width,initial-scale=1">     <script src="https://unpkg.com/maplibre-gl@1.15.2/dist/maplibre-gl.js"></script>     <link rel="stylesheet" href="https://unpkg.com/maplibre-gl@1.15.2/dist/maplibre-gl.css">     <style>       <!-- Popup内のスタイル -->       .popup span {         display: inline-block;         width: 4.5em;       }     </style>   </head>   <body>     <div id="map" style="width: 800px; height: 600px"></div>     <script>       // 地理院ベクトルタイルを表示       let map = new maplibregl.Map({         container: 'map',         style: 'https://gsi-cyberjapan.github.io/gsivectortile-mapbox-gl-js/std.json',         center: [135.0, 35.0],         zoom: 13,         minZoom: 4,         maxZoom: 17,       });       map.on('load', () => {         // 2つのPointを描く         map.addSource('source_points', {           type: 'geojson',           data: {             'type': 'FeatureCollection',             'features': [               {                 'type': 'Feature',                 'geometry': { 'type': 'Point', 'coordinates': [135.0, 35.0],},                 'properties': {                   'id': 'point001',                   'name': 'Bellybutton',                 }               },               {                 'type': 'Feature',                 'geometry': { 'type': 'Point', 'coordinates': [134.99, 35.012],},                 'properties': {                   'id': 'point002',                   'name': 'Route 175',                 }               },             ]           }         });         // Pointを描くレイヤーでスタイルを指定         map.addLayer({           'id': 'layer_points',           'type': 'circle',           'source': 'source_points',           'layout': {},           'paint': {             'circle-color': '#0000FF',             'circle-radius': 20           }         });       });       // 地図上でクリックしたときの処理       map.on('click', (evt) => {         // クリック位置の経緯度         let lng = evt.lngLat.lng;         let lat = evt.lngLat.lat;         // クリック位置のFeature群を取得/レイヤーを限定している         let features = map.queryRenderedFeatures(evt.point, {layers: ['layer_points']});         // 取得したFeature群の属性等をコンソールに表示する         features.forEach((feature) => {           if (typeof feature !== 'undefined') {             let property = '<span>ID</span>' + ':' + feature.properties.id + '<br>'+                            '<span>Name</span>' + ':' + feature.properties.name + '<br>' +                            '<span>Type</span>' + ':' + feature.geometry.type + '<br>' +                            '<span>Location</span>' + ':' + feature.geometry.coordinates;             let popup = new maplibregl.Popup({               closeOnClick: false,               className: 'popup'             })               .setLngLat([lng, lat])               .setHTML(property)               .addTo(map);           }         });       });     </script>   </body> </html> | 
内容
Popupには任意のHTMLを書けるが、ここではqueryRenderedFeaturesで取得した地物の属性を出力している。複数の地物が捕捉された場合、その分だけのポップアップが表示される。Popupは内容に合わせて幅や高さが調整される。
| 1 2 3 4 5 6 7 | let popup = new maplibregl.Popup({   closeOnClick: false,   className: 'popup' })   .setLngLat([lng, lat])   .setHTML(property)   .addTo(map); | 
Popup生成時の引数で、クリック時の消去をfalseにして、スタイル設定用のHTMLクラス名を設定している。
その後、生成したPopupインスタンスの位置と表示内容をセットして、地図に貼り付けている。
ポップアップ内で桁を揃えるためのスタイルは、head要素内で定義している。
| 1 2 3 4 5 6 7 |     <style>       <!-- Popup内のスタイル -->       .popup span {         display: inline-block;         width: 4.5em;       }     </style> | 











