OpenLayersにバルーン(ポップアップ)という機能は無いので自前でつくったバルーンに見える要素を表示する。
バルーンはnew ol.Overlay()……で作ったオーバーレイをmapに予め登録しておき、mapのクリック時にそれの座標を設定することで表示。
バルーン表示用の専用ではなく、単にバルーンのように見えるDOM要素をOverlayとして登録しておいて、それをクリック時に指定位置に置いているだけである。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>地図表示</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- OpenLayers 5.3.0 -->
<link href="lib/ol-popup.css" rel="stylesheet" media="all" />
<link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet" type="text/css">
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script><!-- IE11対応 -->
<title>OpenLayers example</title>
<style>
#map {
height: 100%;
width: 100%;
}
/* バルーン用スタイル */
.ol-popup {
position: absolute;
background-color: white;
-webkit-filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
padding: 15px;
border-radius: 10px;
border: 1px solid #cccccc;
bottom: 12px;
left: -50px;
min-width: 280px;
}
.ol-popup:after, .ol-popup:before {
top: 100%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}
.ol-popup:after {
border-top-color: white;
border-width: 10px;
left: 48px;
margin-left: -10px;
}
.ol-popup:before {
border-top-color: #cccccc;
border-width: 11px;
left: 48px;
margin-left: -11px;
}
.ol-popup-closer {
text-decoration: none;
position: absolute;
top: 2px;
right: 8px;
}
.ol-popup-closer:after {
content: "×";
}
</style>
<script>
var KML_URL = "./Placemarks.kml";
$(function() {
// 地図表示
map = createMap();
// KML表示
var kmlLayer = createKmlLayer();
map.addLayer(kmlLayer);
// バルーン表示設定
var popup = createPopup("#popup");
map.addOverlay(popup);
kmlLayer.once("change", function() { // 以下のようにイベント処理に入れないと,データが読まれずにうまくいかない
// すべてのポイントが表示されるように表示変更
var view = map.getView();
view.fit(kmlLayer.getSource().getExtent(), map.getSize());
if (view.getZoom() > 18) { view.setZoom(18); }
});
map.on("singleclick", function(evt) {
var features = map.getFeaturesAtPixel(evt.pixel);
if (features == null) { return; }
var feature = features[0];
var html = '';
html += '<h1>$[name]</h1>';
html += '<div>$[description]</div>';
html += '<div><img src="$[imagePath]" style="width:100%; max-width:300px;"></div>';
html = html.replace("$[name]" , feature.get("name"));
html = html.replace("$[description]", feature.get("description"));
html = html.replace("$[imagePath]" , feature.get("imagePath"));
popup.getElement().querySelector("div.ol-popup-content").innerHTML = html;
popup.setPosition(evt.coordinate);
});
});
// 地図作成
function createMap() {
return new ol.Map({
target: "map",
layers: [
new ol.layer.Tile({
source: new ol.source.XYZ({
url: "https://cyberjapandata.gsi.go.jp/xyz/pale/{z}/{x}/{y}.png", // 国土地理院 地図タイル
projection: "EPSG:3857"
})
})
],
view: new ol.View({
center: ol.proj.fromLonLat([132.4553011, 34.3986543]),
zoom: 14
})
});
}
// KMLレイヤー作成
function createKmlLayer() {
return new ol.layer.Vector({
source: new ol.source.Vector({
url: KML_URL + "?" + (new Date()).getTime(), // キャッシュ無視のため時刻をつける
format: new ol.format.KML({ extractStyles: true, extractAttributes: true, showPointNames: false }), // showPointNames: false で横の名前表示を無しにする
})
});
}
// バルーン作成
function createPopup(popupSelector) {
// ol.OverlayがDOM構成を組み替えるようなので先に要素を取得しておかないとうまく取得できなくなる
var popupElement = document.querySelector(popupSelector);
var closerElement = document.querySelector(popupSelector + " .ol-popup-closer");
var popup = new ol.Overlay({
element: popupElement,
autoPan: true,
autoPanAnimation: { duration: 250 }
});
closerElement.addEventListener("click", function() {
popup.setPosition(undefined);
closerElement.blur();
return false;
});
return popup;
}
</script>
</head>
<body>
<div id="map"></div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div class="ol-popup-content"></div>
</div>
</body>
</html>