Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
React + Firebase でヒートマップを実装する
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
takf
January 13, 2018
Programming
2.1k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
React + Firebase でヒートマップを実装する
2018.01.12 まぼろしのJS勉強会 #2 「細かすぎて伝わらないUI実装選手権」
takf
January 13, 2018
More Decks by takf
See All by takf
Go未経験・MVC脳のエンジニアが オニオンアーキテクチャを学ぶまで
takfjp
0
48
見やすい PRを作るために取り組んでいること
takfjp
0
70
Denoに入門していきなりAleph.jsを触ってみた
takfjp
0
550
Atomic Design とテストの○○な話
takfjp
2
2k
Node.jsのアップグレードで気をつけたこと
takfjp
1
3k
FARM スタックに触れてみる
takfjp
0
1.7k
React Testing Library の Query について整理してみた
takfjp
0
570
React.js 消えるライフサイクルメソッドについて
takfjp
0
170
Laravel 初めての業務で遭遇したハマりポイント×2
takfjp
2
3.2k
Other Decks in Programming
See All in Programming
標準パッケージに uuid が追加された 背景から見る Go らしい意思決定 / go_127_uuid_decision
convto
5
9.2k
Ghostty + Neovimで作る 透明でカッコ良い開発環境
j341nono
0
150
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
100
すこし踏み込む CancellationToken
htkym
2
1.5k
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
280
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
320
Are APIs Still Relevant in the AI Era?
soyuka
0
370
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
4
320
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
180
App Intentsのビルドプロセスを支える技術
kntkymt
0
480
AWSに止められる覚悟してますか?
morizo_1984
2
490
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
2.5k
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
620
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
600
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
sira's awesome portfolio website redesign presentation
elsirapls
0
430
The Language of Interfaces
destraynor
162
27k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
Raft: Consensus for Rubyists
vanstee
142
7.7k
HDC tutorial
michielstock
2
930
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
290
Transcript
React + Firebase で ヒートマップを実装する 2018.01.12 まぼろしのJS勉強会 #2 「細かすぎて伝わらないUI 実装選手権」
今日発表する事・つかうもの Reactをベースにヒートマップを実装します ・データベース:Firebase Realtime Database ・Firebase CLI ・地図API:Leaflet.JS ・↑のヒートマップ用ライブラリ:leaflet-heatmap +
heatmap.js ・ジオコーディング用ライブラリ:leaflet-geosearch
Leaflet.JSについて(http://leafletjs.com/) JSで動く軽量なオープンソースの地図ライブラリ OpenStreetMapをベースにしている (https://www.openstreetmap.org/) GoogleMapsAPIなどと同様にマーカー、ポップアップを実装可能 MapTilerというアプリを使えばオリジナル地図を実地図に重ね合わせることもできる (https://www.maptiler.com/)
江ノ電の各駅の利用者数を地図上で可視化したい!
データ取得の流れ(理想形) JSON ・駅名 ・平均乗降客数 / 日 Realtime Database 地図APIにプッシュ Geocoding
(駅名から 緯度・経度を取得)
実際の実装は・・・ JSON Realtime Database 再びDBにプッシュ Geocoding (駅名から 緯度・経度を取得) 緯度・経度を取得、 データを加工
Firebaseへの接続方法
None
//親コンポーネント class App extends Component { componentDidMount(){ handleMap() //DOMがマウントされた後に Mapを扱う関数を呼び出し }
render() { return ( <div className="App"> <div id="map"></div> //Mapを扱うために空のdivを作る <Footer /> //「Footer」と表示するだけのコンポーネント </div> ); } } export default App;
Reactにおける地図の実装 ・地図用の<div>を親コンポーネントでrender() ー> componentDidMount() で地図の呼び出し -> 地図単体は実DOMとして扱う
import L from ‘leaflet’ let heatmapData = { max: 100000,
min: 0, data: [] } export const handleMap = () => { //表示範囲の固定 const bounds = new L.LatLngBounds( new L.LatLng(35.33862, 139.48608), new L.LatLng(35.30338, 139.55258)) //地図のタイルレイヤ const baseLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' }) //ヒートマップのレイヤ const heatmapLayer = new HeatmapOverlay({ container: document.getElementById('map') }) //<div id=’map’>にマウントする
const heatmapRef = firebaseApp.database().ref('heatmapVal') //FirebaseのDBを購読 heatmapRef.once('value', (snapshot) => { //
snapshot で中身を引き抜くことができる if(snapshot) { const val = snapshot.val() Object.keys(val).forEach((key) => { let j = Math.random() heatmapData.data.push({lat: Number(val[key]['lat']), lng: Number(val[key]['lng']), count: val[key]['count']*10}) //緯度・経度・データ量を配列にプッシュ }) heatmapLayer.setData(heatmapData) //ヒートマップにデータをセット } }) //Mapの定義 let map = new L.map('map', { // import L from ‘leaflet’ center: {lat:35.31625, lng:139.51852}, zoom: 18, layers: [baseLayer, heatmapLayer] }) //地図のタイルレイヤとヒートマップのレイヤを重ねる .fitBounds(bounds) }
なぜかこうなってしまった
地図APIの難しい点 ・Reactは仮想DOMを扱うのに対し、地図は実DOMである -> Reactのコンポーネントと地図上のデータを連携させるにはやや苦労がある ・React-Leaflet というライブラリがある程度吸収してくれるけど・・・ -> 実DOMをReactコンポーネントっぽく書けるが痒いところには手が届かない ・地図の操作で意図しないライフサイクルが動く場合があり、注意が必要 ->
render()の中になるべくコールバックを記述しないように
だけど地図は面白い!