Classを使ったプログラミングモデル¶
maprayJSはゲームエンジンライクな設計思想で設計されています。機能拡張をやりやすいように、基本クラスを派生することで、独自の機能を追加できる仕組みになっています。coreエンジンの機能拡張ももちろんできます。ここでは、このプログラミングモデルを体験するために、StandardUIViewerを拡張してみましょう。maprayJSを使うほぼ全てのエンジニアはStandardUIViewerを拡張したくなりますので、これをクラス化します。
StandardUIViewerの拡張¶
なぜStandardUIViewerを拡張するのか¶
StandardUIViewer
は、maprayJSの標準的なビューワー実装ですが、プロジェクト固有の要件に応じて以下のような機能を追加したくなります:
- 独自のカメラ制御
- カスタムUIの統合
- 特定のイベント処理
- アニメーションの追加
- データの動的な読み込みと更新
基本的な拡張方法¶
class MyViewer extends mapray.StandardUIViewer {
constructor(container, access_token, options = {}) {
super(container, access_token, options);
// 独自の初期化処理
this.initializeCustomFeatures();
}
initializeCustomFeatures() {
// カスタム機能の初期化
console.log("MyViewer initialized");
}
// フレーム更新時の処理をオーバーライド
onUpdateFrame(delta_time) {
super.onUpdateFrame(delta_time);
// 独自の更新処理
this.updateCustomLogic(delta_time);
}
updateCustomLogic(delta_time) {
// 毎フレーム実行される独自の処理
}
// カスタムメソッドの追加
flyToLocation(latitude, longitude, height) {
const camera = this.viewer.camera;
// カメラを指定位置に移動
camera.position.set(longitude, latitude, height);
}
}
具体的な実装は、この後のハンズオンのコンテンツで幾つかを扱いますので、ここでは、まず、これまで作成したコードをクラス化してみましょう。
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 |
|
これで、StandarUIViewerの関数をOverrideして機能を拡張することができます。