Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a simple MapLibre GL JS site with hash and PMTiles enabled, which takes style.json URL. #548

Closed
hfu opened this issue Jul 9, 2024 · 12 comments
Assignees

Comments

@hfu
Copy link
Contributor

hfu commented Jul 9, 2024

FOIL4G で style.json の URL を改めてリソースとしてとらえやすくなっていることを踏まえ、@hfu の実用的モチベーションもあり、次の要件を満たすシンプルな MapLibre GL JS サイトを作りたいと思っています。

MapLibre GL JS は最新のものに自動的に追従するようにしたいです。

このようなサイトを作るにあたり、注意するべきこと、その他コメントなどあればいただけますか。

@asahina820 @yuiseki @smellman @albertkun @k96mz

私のユースケース

  • https://observablehq.com/d/e66dd18a3be303c7 から style.json をエクスポートし、GitHub Pages なりにアプロードして、その URL を ?style に指定して地図を共有する。
  • Observable では hash が上手く使えないと思っており、ピンポイントを指定して地図を共有することができないのが、このサイトの構築を考えているきっかけです。
@hfu
Copy link
Contributor Author

hfu commented Jul 10, 2024

https://optgeo.github.io/terrain22/style.json をサンプルに使っていただければと思います。日本の地形分類も入っています。

@asahina820
Copy link
Collaborator

MapLibre GL JS は最新のものに自動的に追従するようにしたいです

CDNを使ってMapLibre のライブラリを読み込む場合に、バージョンを指定しないと最新バージョンが適応されます。
https://zenn.dev/asahina820/books/c29592e397a35b/viewer/0200eb#%E8%A7%A3%E8%AA%AC

@hfu
Copy link
Contributor Author

hfu commented Jul 10, 2024

ChatGPT と相談して書いてみました。(PMTiles の扱いを除く。)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>style view</title>
<link href="https://unpkg.com/maplibre-gl/dist/maplibre-gl.css" rel="stylesheet" />
<style>
  body { margin: 0; padding: 0; }
  #map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script src="https://unpkg.com/maplibre-gl/dist/maplibre-gl.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
    const urlParams = new URLSearchParams(window.location.search)
    const styleUrl = urlParams.get('style') || 'https://optgeo.github.io/terrain22/style.json'

    const map = new maplibregl.Map({
        container: 'map',
        style: styleUrl,
        hash: true
    })
})
</script>
</body>
</html>

夕食後、リポジトリを作って本格に始めてみます。

@albertkun
Copy link
Collaborator

多分、pmtilesのことはこのmaplibre.glのドキュメントでいいかな? https://maplibre.org/maplibre-gl-js/docs/examples/pmtiles/

@hfu がレポジトリー作った後で僕が調べって見ようかも! 

@hfu
Copy link
Contributor Author

hfu commented Jul 10, 2024

@albertkun ありがとう!Observable 上では https://observablehq.com/d/e66dd18a3be303c7

maplibregl = {
  let m = await require("maplibre-gl")
  let p = await import("https://cdn.skypack.dev/pmtiles")
  let protocol = new p.Protocol()
  m.addProtocol('pmtiles', protocol.tile)
  yield m
}

という形でロードできているので、これに沿って ES モジュールとして pmtiles をインポートするコードを書いてみます。

@hfu
Copy link
Contributor Author

hfu commented Jul 10, 2024

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>style view</title>
<link href="https://unpkg.com/maplibre-gl/dist/maplibre-gl.css" rel="stylesheet" />
<style>
  body { margin: 0; padding: 0; }
  #map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script src="https://unpkg.com/maplibre-gl/dist/maplibre-gl.js"></script>
<script type="module">
import { Protocol } from 'https://cdn.skypack.dev/pmtiles'
maplibregl.addProtocol('pmtiles', (new Protocol()).tile)

document.addEventListener('DOMContentLoaded', function () {
    const urlParams = new URLSearchParams(window.location.search)
    const styleUrl = urlParams.get('style') || 'https://optgeo.github.io/terrain22/style.json'

    const map = new maplibregl.Map({
        container: 'map',
        style: styleUrl,
        hash: true
    })
})
</script>
</body>
</html>

これで進めてみます。

@hfu
Copy link
Contributor Author

hfu commented Jul 10, 2024

unopengis/stylehub という名前でリポジトリ作ります。

@hfu
Copy link
Contributor Author

hfu commented Jul 10, 2024

うまくいきました!

https://unopengis.github.io/stylehub/#9.55/14.0564/101.1642/-133/59

スクリーンショット 2024-07-10 20 43 21

@hfu
Copy link
Contributor Author

hfu commented Jul 10, 2024

@hfu
Copy link
Contributor Author

hfu commented Jul 10, 2024

リポジトリは https://github.com/unopengis/stylehub でした。うまくいっているようなのでイシューはクローズします。
ありがとうございました!

@hfu
Copy link
Contributor Author

hfu commented Jul 10, 2024

ここで作ったシンプルサイトをベースに、Observable から terrain22 の凡例を表示するコードを追加し、 https://optgeo.github.io/terrain22/index.html にする。

@hfu hfu reopened this Jul 10, 2024
@hfu
Copy link
Contributor Author

hfu commented Jul 11, 2024

実装できました!
https://optgeo.github.io/terrain22/#10.85/32.8637/131.0435/47.2/59
スクリーンショット 2024-07-11 21 32 13

@hfu hfu closed this as completed Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants