GeoJSON形式の地物情報をLeafletに取り込みます。
GeoJSONファイルの準備
簡単なGeoJSONファイルを準備する。
GeoJSONファイルの詳細についてはこちら
使い方
サンプルのdata.geojsonデータを取得して、L.geoJSON関数の引数として、fileオブジェクトを渡します。
fetch("./data.geojson")
.then(response => response.json())
.then(data => {
L.geoJSON(data, {
onEachFeature: function(feature, layer){
let name = feature.properties.name;
layer.bindPopup(name);
}
}).addTo(_map);
});
全体コード
<html lang="ja">
<head>
<title>Leafletのマーカー(GeoJSON)の表示</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css">
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
</head>
<body>
<div id="map" style="width:100%;height:100%"></div>
<script>
//表示位置を設定
var _map = L.map('map');
//初期表示時に中心座標とズームレベルを設定
_map.setView([35.6895, 139.69172], 10);
//初期表示時の表示地図を設定
L.tileLayer('https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png', {
attribution: "<a href='https://maps.gsi.go.jp/development/ichiran.html' target='_blank'>国土地理院</a>"
}).addTo(_map);
//マーカー(GeoJSON)の表示
fetch("./data.geojson")
.then(response => response.json())
.then(data => {
L.geoJSON(data, {
onEachFeature: function(feature, layer){
let name = feature.properties.name;
layer.bindPopup(name);
}
}).addTo(_map);
});
</script>
</body>
</html>
コメント